BradS2S

BradS2S

I can't get Enum.into/2 to work according to spec

I want to preface this by saying that I’m feeling very thankful for all the helpful answers I’ve been getting from the community and feel like I’m to the point where going to the source code first is really productive.

Getting into the difference between Enumerable and Collectable protocols and how they are invoked in the Enum and Stream modules, I ran across something that didn’t make sense to me.

Setting aside the deprecated collectables (non-empty and keyword lists), I understand the following collections to implement the two different protocols:

Enumerable Collectable (supported)
IO.Stream IO.Stream
Range Empty List
List BitString
Map Map
Function MapSet
MapSet Mix.Shell
DateRange File.Stream
Stream Stream
File.Stream

I haven’t been able to reconcile the spec typing with what seems to work.

@spec into(Enumerable.t(), Collectable.t()) :: Collectable.t()
Enum.into(enumerable, collectable)

Some situations probably aren’t that helpful like passing a range into an empty map but should it work if they typing says they should?


Enum.into(1..10//2, %{})
** (FunctionClauseError) no function clause matching in anonymous fn/2 in Collectable.Map.into/1    
    
    The following arguments were given to anonymous fn/2 in Collectable.Map.into/1:
    
        # 1
        %{}
    
        # 2
        {:cont, 1}
    
    (elixir 1.15.7) anonymous fn/2 in Collectable.Map.into/1
    (elixir 1.15.7) lib/enum.ex:1554: anonymous fn/3 in Enum.reduce_into_protocol/3
    (elixir 1.15.7) lib/range.ex:526: Enumerable.Range.reduce/5
    (elixir 1.15.7) lib/enum.ex:1553: Enum.reduce_into_protocol/3
    (elixir 1.15.7) lib/enum.ex:1537: Enum.into_protocol/2
    /Users/bradhanks/membrane_tutorial.livemd#cell:wp5kyzkt6fqb75ktz3vdbl7sbfehlmzc:1: (file)

It makes sense that the Collectable.Map.into/2 expects a tuple and that Collectable.BitString.into/2 only has function clauses that match for binary or bitstring.

I’m looking for a better understanding on what the types in @spec represent.

Marked As Solved

josevalim

josevalim

Creator of Elixir

Hi @BradS2S! You have been submitting PRs to Elixir and if you want to send a PR that improves the error message here, it would be very welcome. Something like:

map_acc, {:cont, {key, value}} -> Map.put(map_cc, key value)
map_acc, {:cont, other} -> raise "collecting into a map requires {key, value} tuples, got: #{inspect(other)}"

Thank you!

Also Liked

lud

lud

I don’t think you can collect a range in a map because a map needs a key and a value.

For instance 1..10//2 |> Enum.map(& {&1, :some_value}) |> Enum.into(%{}) should work.

Edit: but you know that, sorry I did not read your post carefully. Well the types are broad and do not have generics.

I a static language you would have Enumerable.t<Pair> and Collectable.t<Pair> and it would not compile unless the wrapped types are compatible (Pair and Pair are compatible for instance).

LostKobrakai

LostKobrakai

The same is true the other way as well. Maps are an enumerable of tuple-2 values. The same the map collectable implementation expects an enumerable of tuple-2 values as input.

al2o3cr

al2o3cr

Some typings, particularly for generic functions like in Enum, are intended more for human readers than for the compiler.

For instance, both Enumerable.t() and Collectable.t() are generated by Protocol as aliases of term:

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement