cblavier

cblavier

Enum.map_join with a comprehension

Do you think a join option would be a good idea for comprehensions?
Something like Enum.map_join/2 that could map and join lists in a single pass.

book_ids = for book <- books, join: ",", do: book.id

If it looks like a good idea, I will open a ticket / PR.

Most Liked

Eiji

Eiji

Well … decision on adding features and enhancements is up to @Elixir-Core-Team. What I can say is that you can already do that in a single pass using reduce option:

# Here is a simplified list
books = [%{id: 1}, %{id: 2}, %{id: 3}, %{id: 4}, %{id: 5}]

# Here is an example comprehension:
book_ids = for book <- books, reduce: "" do
  "" -> book.id
  acc -> "#{acc}.#{book.id}"
end

Helpful resource:
:reduce option in Kernel.SpecialForms.for/1

adamu

adamu

I think he means why doesn’t join take a function that’ll also map it for you, like into, or Map.new does?

gregvaughn

gregvaughn

You could write your own Collectable module and use :into to achieve this if you expect to use it often.

Last Post!

sabiwara

sabiwara

Elixir Core Team

My guess would be because there already is a joiner optional argument, adding an extra optional mapper argument would have made it awkward to use?

join(enumerable, joiner \\ "", mapper \\ fn x -> x end)

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement