nhpip
I know this is a really minor peeve, but why is this the case?:
iex(cs@localhost)54> %{key1: :i_am_a_map_whooot} |> Enum.map(&(&1))
[key1: :i_am_a_maaa___list__what_the?]
Yes I know that all I need to do is |> Enum.into(%{}) but why? Why shouldn’t the ouput type be the same as the input type?
I’m sure this has been asked 1000 times before.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New
Other Trending Topics
Hi everyone! I’d like to share Fude (筆, “brush”), a charting library for Phoenix LiveView.
I built it for dashboards and reports where y...
New
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
You don’t have to. You can call reduce over new map like this:
Note: The order of map elements is not guaranteed to be sorted.
nhpip
But, I don’t know, seems like extra work. I’m so used to it that it doesn’t bother me that much, I just thought that it was an odd implementation detail.
Maybe we need an
Enum.fmap/2like Haskell?dimitarvp
Using
Enum.maporEnum.reduceand working with two-tuples and then just doingMap.newdoes not justify the need for a function that does the same IMO. Just do your stuff and slap aMap.newat the end.sodapopcan
One thing is that the result of your map operation is in a predictable order. I don’t have a real world example of where this would particularly useful, but in a pipeline it could be. Having each result getting converted back to a map would be pretty chaotic.
Eiji
Oh, right I forgot about it. Unfortunately we do not have a way to map the
Mapwithout converting it first to a list, see:https://github.com/elixir-lang/elixir/blob/349321117ebb598cd10a4f22e8ea8e1c4b13b6c8/lib/elixir/lib/enum.ex#L4879-L4881
https://github.com/elixir-lang/elixir/blob/41f6ee3f2e605f7066b23d96362e3b0c620c565b/lib/elixir/lib/map.ex#L248-L264
So at the very end we do exactly the same.
dimitarvp
Yep, I also checked the Elixir source a while ago. IMO doing
Map.newonly once is more performant than doing N timesMap.putso I am always doing that.Marcus
As mentioned before,
Enum.mapon a map returns a list because this would fit better to pipes without the need to convert to a list and back behind the scenes in every step.There used to be the
Map.map/2function in Elixir, which is now deprecated. But you get the same result withMap.new/2.BartOtten
Except when the input is a map, there is no guarantee about order in the first place. So this argument doesn’t seem to hold.
For the argument that conversion would take place between steps in a pipe; why would it? Elixir can detect it is in a pipeline and not convert in between as an optimization.
Once you know the ‘conversion’ it does not matter that much anymore. But I see how returning the same type as the input makes sense from a developer experience.
wojtekmach
The output type of
Enum.map(and mostEnumfunctions) cannot be the same as the input type because Enum works on a variety of data types through theEnumerableprotocol. A simple example is:We couldn’t possibly keep the shape of the input,
%Range{}, because the end result would be nonsensical. Some other things that implement Enumerable protocol are file streams and even infinite streams.As was mentioned before there used to be a
Map.map/2but it got deprecated because a similar function existed all along, Map.new/2:so yeah, long story short, in your particular case instead of
Enum.map/2useMap.new/2.BartOtten
Seems an implementation detail in the case of Range. From an outside perspective the range simply expands to a list so a list in return makes sense. As far as I can see the sole reason for having a ‘Range’ struct during processing is optimization, and explicit use of the struct is deprecated.
—-snip this part: Too early in the morning—-
—- and the start was incorrect too, see below —-