BradS2S

BradS2S

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.

Showing Posts 1 to 7

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).

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

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.

BradS2S

BradS2S OP

Yeah that was going to be my next question. I held off just in case the answer to this resolved the other. Thank you for clearing that up!

Arguably there isn’t much utility to the change but it would be tidier if there was some sort of provision for defaults when required.

fn 
# map_acc, {:cont, {:default_key, default_key, value }} -> Map.put(map_cc, default_key, value)
map_acc, {:cont, {key, value}} -> Map.put(map_cc, key value)
map_acc, :done -> map_acc
_map_acc, :halt -> :ok:
BradS2S

BradS2S OP

Yeah there seem to be many instances when it isn’t going to work which is why it was so disconcerting. But I’m not that bothered if no one else is. There’s always into/3.

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!

BradS2S

BradS2S OP

Thank you! I’ll do it. :grinning:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews