I believe it is relying on the default reduce-based implementation for lists (because a loop cannot be avoided for linked lists) while there is a more efficient way for maps to count or check memberships. The fallback happens here: https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/enum.ex#L608
It is explained in the Enumerable documentation here: https://hexdocs.pm/elixir/Enumerable.html This protocol requires four functions to be implemented, reduce/3 <https://hexdocs.pm/elixir/Enumerable.html#reduce/3>, count/1 <https://hexdocs.pm/elixir/Enumerable.html#count/1>, member?/2 <https://hexdocs.pm/elixir/Enumerable.html#member?/2>, and slice/1 <https://hexdocs.pm/elixir/Enumerable.html#slice/1>. The core of the protocol is the reduce/3 <https://hexdocs.pm/elixir/Enumerable.html#reduce/3> function. All other functions exist as optimizations paths for data structures that can implement certain properties in better than linear time. Le mardi 8 septembre 2020 à 08:51:52 UTC+9, [email protected] a écrit : > I came across this piece of code which does Enumerable protocol > implementation for Lists - > https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/enum.ex#L3761-L3763 > > Consequently, some lines below the same implementation of Maps have this - > https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/enum.ex#L3783-L3798 > > Curious why Lists should return error while Maps return values being part > of the same protocol implementation. > > -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/9a49599b-ea41-4103-b746-55cd7335d931n%40googlegroups.com.
