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.

https://github.com/elixir-lang/elixir/blob/7533e56c1baec77c2aa1a4c0ad02730fae464201/lib/elixir/lib/enum.ex#L1259C11-L1259C11

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:

https://github.com/elixir-lang/elixir/blob/3ae8475e41bb1ebe81c2e0954c88381ebe31afab/lib/elixir/lib/protocol.ex#L899-L904

Where Next?

Popular in Questions Top

mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement