mashhadi

mashhadi

I have an list which is like

[[1, 5], [3, 12], [4, 10]]

i want to change it to a map like below

%{
1 => 5,
3 => 12,
4 => 10
}

Currently i am using following code and my question is , is there a better way to do. i mean if there is more concise way, or may be a built-in function to do so.

Enum.reduce([[1, 5], [3, 12], [4, 10]],%{}, fn row, acc ->
        acc
        |> Map.put(List.first(row), List.last(row))

Showing Posts 1 to 8

fuelen

fuelen

data = [[1, 5], [3, 12], [4, 10]]
Enum.into(data, %{}, fn [a,b] -> {a, b} end)
kokolegorille

kokolegorille

While Enum.into is the simplest, You could have rewritten your Enum.reduce like this.

list = [[1, 5], [3, 12], [4, 10]]
Enum.reduce(list, %{}, fn [k, v], acc -> Map.put(acc, k, v) end)

# or the for version of it

for [k, v] <- list, reduce: %{} do acc -> Map.put(acc, k, v) end
NobbZ

NobbZ

It’s quite uncommon to use a list of lists like this, a more common approach would be to have the list as a list of binary tuples.

So if you have control over the shape of the incoming list, make it [{1, 2}, {3, 4}], then it’s easy as calling Map.new(list).

Having said that, you can also use Map.new/2 and List.to_tuple/1 as this with your initial list shape: Map.new(list, &List.to_tuple/1).

lud

lud

Today I learnt that for has a reduce option and it’s great !

Thanks :slight_smile:

romenigld

romenigld

I tried this code that I never saw:

And I have this error:
** (CompileError) iex:3: unsupported option :reduce given to for

NobbZ

NobbZ

Its only available since elixir 1.8. You really should update.

romenigld

romenigld

It’s true thank’s.

dimitarvp

dimitarvp

I would not use Map.put or Enum.into because that means that the runtime copies the map, adds one more pair to it, then throws out the previous one – N times. That’s very inefficient.

Go with what @NobbZ suggested: it basically says “construct a new map using this list and then use this function (the second parameter, in this case List.to_tuple) to convert each element of the list to a key/value tuple” – which is the format that Map.new expects.

For (supposedly) even bigger efficiency I’d write this:

Map.new(your_list, fn [k, v] -> {k,v} end)

This has the added benefit that it will break if your list is malformed, namely you want a list of sublists and each of the sublists must be exactly 2 elements long. If there’s even one sublist that is not 2-sized then you’ll get a runtime error. Annoying, sure, but you’ll know you have a bug very quickly and can fix it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
peck
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
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
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
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
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

Other Trending Topics Top

mudasobwa
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
KristerV
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews