script

script

I have a list of tuple like this

  tuple =   [{"id", "provider_id"}, {"cost", "price"}] .  

And i have list of maps:

    list_of_maps =[
    %{cost: 211.0, id: 1},
    %{cost: 149.0, id: 2},
    %{cost: 247.0, id: 3},
    %{cost: 137.0, id: 4},
    %{cost: 272.0, id: 5},
    %{cost: 150.0, id: 6}
   ]

I want to replace id with provider_id and cost with price by using the the list of tuples in this map.

How can it be done?

Thanks

Showing Posts 1 to 5

grych

grych

Creator of Drab

To solve (any) issue the best is to divide it into smaller steps.
I’ve created a function replacement_for which returns the corresponding string from tuple, ie. for :id it will return "provider_id" and so on.
Then, the function replace_keys which replaces the keys for the single map.

defmodule R do
  defp replacement_for(key, tuple) do
    tuple
    |> Enum.find(fn {x, _} -> x == to_string(key) end)
    |> elem(1)
  end

  def replace_keys(map, tuple) do
    for {k, v} <- map, into: %{}, do: {replacement_for(k, tuple), v}
  end
end

All you need to do now, is just use the replace_keys in Enum.map (or for comprehension, as you wish):

Enum.map(list_of_maps, fn m -> R.replace_keys(m, tuple) end)
script

script OP

Thanks

gausby

gausby

Is the key mapping something that needs to change dynamically, because can be done like so:

for %{id: id, cost: cost} <- list_of_maps, do: %{provider_id: id, price: cost}
Eiji

Eiji

@script: Here is another and also shortest solution I know about:

Enum.map(list_of_maps, & %{price: &1.cost, provider_id: &1.id})

For more information, see:

  1. Kernel.SpecialForms.&/1
  2. Enum.map/2
peerreynders

peerreynders

FYI: This topic is a continuation of Why doesn't ecto support dynamic map query

In particular:

I want dynamic solution.

otherwise

query = from p in Qber.V1.JobModel, where: p.id == 1, select: %{provider_id: p.id, price: p.cost}

would have been an acceptable solution in the first place.


Another alternative:

defmodule Demo do

  # 1) "tuple" is already in a format ready to
  #    be converted to a Map which can be used repeatedly
  #    over multiple rows/maps for the purpose of a key lookup
  # 2) Return a function that "knows" the key mappings
  #    via the to_new_key map in the function closure.
  # 3) We're avoiding turning strings into atoms as
  #    atoms aren't garbage collected. So we prefer
  #    ways where we turn atoms into strings.
  #    Note: Kernel.to_string/1 relies on the
  #          String.Chars protocol.
  #          If we know that we will always be using
  #          atoms, Atom.to_string seems more appropriate.
  # 4) We are not really "replacing" keys. We are only
  #    selecting values found under a key specified
  #    in the to_new_key lookup - and only if we find
  #    it, do be store it in new_map.
  #    4a) the original key is found, the value is stored
  #        under new_key in new_map.
  #    4b) the original key IS NOT FOUND
  #        SO THE value IS IGNORED - we simply
  #        return the existing new_map.
  #        FOR "replacement" we would either need
  #            Map.put(new_map, key, value)
  #        or possibly
  #            Map.put(new_map, Atom.to_string(key), value)
  #
  def make_mapper(key_list) do
    to_new_key = Map.new(key_list)

    fn ({key, value}, new_map) ->
      case Map.fetch(to_new_key, Atom.to_string(key)) do
        {:ok, new_key} ->
          Map.put(new_map, new_key, value) # (4a)
        _ ->
          new_map                          # (4b)
      end
    end
  end

  def transform(row_list, key_list) do
    #
    # 5) create key_mapper function to reuse on each map/row in row_list
    #
    key_mapper = make_mapper(key_list)
    for row <- row_list,
      do: Enum.reduce(row, %{}, key_mapper)
  end

end

tuple =   [{"id", "provider_id"}, {"cost", "price"}]
list_of_maps =[
  %{cost: 211.0, id: 1},
  %{cost: 149.0, id: 2},
  %{cost: 247.0, id: 3},
  %{cost: 137.0, id: 4},
  %{cost: 272.0, id: 5},
  %{cost: 150.0, id: 6}
]

result = Demo.transform(list_of_maps, tuple)

IO.inspect(result)
$ elixir demo.exs
[
  %{"price" => 211.0, "provider_id" => 1},
  %{"price" => 149.0, "provider_id" => 2},
  %{"price" => 247.0, "provider_id" => 3},
  %{"price" => 137.0, "provider_id" => 4},
  %{"price" => 272.0, "provider_id" => 5},
  %{"price" => 150.0, "provider_id" => 6}
]
$
— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
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
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
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
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews