billposters

billposters

Enum.reduce for idiots

I have a list of maps that provide a type of property lookup, and a map with values.

lookup =
  [
    %{key: "foo", type: :string},
    %{key: "bar", type: :string},
    %{key: "baz", type: :string}
  ]

params = %{foo: "bananas", baz: "apples}

What I’ve been attempting to do (unsuccessfully) is to use Enum.reduce, for lack of a better approach, in order to combine the two into the following format, while discarding any lookup keys that aren’t present in values:

output =
  [
    %{key: :foo, value: "bananas", type: :string},
    %{key: :baz, value: "apples", type: :string},
  ]

I had tried this, but I can’t work out how to access the value from params using the atom key from lookup.

def clever_merge(lookup, params) do
  Enum.reduce(lookup, [], fn(%{key: k, type: t}, output) ->
    with rec <- %{key: String.to_atom(k), value: params.k, type: t} do
      List.insert_at(output, -1, rec)
    end
  end)
end

The other issue is filtering out keys that aren’t present in params. I tried a few combinations of if statements around Enum.reduce but I couldn’t work it out.

I know it’s always a big ask but could someone point me in the right direction please?

Thanks.

Marked As Solved

brettbeatty

brettbeatty

I would actually swap the roles of your lookup and params variables. If order doesn’t matter, you could just turn lookup into a map and iterate over params to create your expected output.

lookup = [
  %{key: "foo", type: :string},
  %{key: "bar", type: :string},
  %{key: "baz", type: :string}
]
lookup = Map.new(lookup, &{&1.key, &1.type})
#=> %{"bar" => :string, "baz" => :string, "foo" => :string}

for {key, value} <- params,
    {:ok, type} = Map.fetch(lookup, to_string(key)) do
  %{key: key, value: value, type: type}
end

Also Liked

ityonemo

ityonemo

I think you shouldn’t use Enum.reduce for this. Enum.flat_map is better, since you are going from list to list and possibly omitting some.

lookup = [
    %{key: "foo", type: :string},
    %{key: "bar", type: :string},
    %{key: "baz", type: :string}
 ]

params = %{"foo" => "bananas", "baz" => "apples"}

Enum.flat_map(lookup, fn 
  map when is_map_key(params, :erlang.map_get(:key, map)) ->
    [Map.put(map, :value, params[map.key])]
  _ -> []
end)
billposters

billposters

Thanks for everybody’s help. I hadn’t thought of swapping roles and looping over the params.

But it was great to learn from all the different takes on using Enum. It’s been a real benefit, so thank you.

iangl

iangl

The answer above is very straightforward and I think should work pretty well!

If you do wanna go with reducing because the order matters, I think this should work:

Enum.reduce(lookup, [], fn %{key: key} = map, acc ->
  if Map.has_key?(params, key) do
    value = params[key]
    acc ++ [Map.put(map, :value, value)]
  else
    acc
  end
end)

Have not tested it, but I’d believe it will work

sribe

sribe

You don’t say what error you might have gotten, but in my quick reading I only see only problem “.” is for accessing fields of records, you want params[key] basically, but of course you need a conditional on whether or not it exists, so you also want to if or case on a check for its existence

Eiji

Eiji

You have 2 mistakes in code:

  1. You are calling params.k expecting that k is variable. params.k works like params[:k] and therefore it would not work for you. Also you cannot mix Atom and String types as they are completely different types.

  2. Your with clause would always raise something like:

    ** (KeyError) key :k not found in: # …
    

    The raison is described in 1st point. However mistake here is to assume that raise would simply fail with clause. Elixir way is “let it fail”. Of course you can write catch/try, but unless you have a strong reason for it it’s generally not recommend.

Here are my 2 proposals:

defmodule Example do
  def safe_sample(list, map) when is_list(list) and is_map(map) do
    list
    |> Enum.reduce([], fn %{key: string_key, type: type}, acc ->
      case Enum.find(map, &custom_has_key?(&1, string_key)) do
        {atom_key, value} -> [%{key: atom_key, type: type, value: value} | acc]
        nil -> acc
      end
    end)
    |> Enum.reverse()
  end

  defp custom_has_key?({atom_key, _value}, string_key) do
    Atom.to_string(atom_key) == string_key
  end

  def unsafe_sample(list, map) when is_list(list) and is_map(map) do
    list
    |> Enum.reduce([], fn %{key: string_key, type: type}, acc ->
      atom_key = String.to_atom(string_key)

      case map do
        %{^atom_key => value} -> [%{key: atom_key, type: type, value: value} | acc]
        _ -> acc
      end
    end)
    |> Enum.reverse()
  end
end

There are 2 major differences between safe and unsafe versions:

  1. safe is good for data from untrusted source like user input
  2. unsafe is faster because we have instead of Enum.find/2 we are using pure pattern matching which is optimized by compiler

In case we are sure about existence of string_key anywhere in params (as Atom) we can use String.to_existing_atom/1 which is safe, but in case like yours it would raise ArgumentError and that’s why I have not provided a safe way with optimized pattern-matching usage.

Code you wrote is good in theory and may be bad in practice. :slight_smile:

You are doing something (swapping roles of 2 variables) without a discussion with author of topic. This is called assumption and in theory there is nothing bad. However take in mind that there are no 2 exactly same people and it’s only matter of time when you would expect different behaviour.

In this example you reached expected result and therefore there is nothing bad in your code now. I can see 2 cases when assumption would cause trouble:

  1. On prod data - pay attention that every data posted on forum (and on source with public access) should be redacted, so the code which works here does not need to works in prod.

  2. Even if assumption works in practice it may fail when enhancing original theory. Let’s take an example. Somebody asked you to make a decorations and you should use eggs for it. Mostly by assumption of easterday painting eggs is just “normal”, but if we do not want them for easterday even if they would be worth thousands of USD we may need new eggs for decorations.

This one is unfortunately a popular mistake. Using [head | tail] optimization along with Enum.reverse/1 is faster than appending two lists like in your example.

You see well, but you do not everything. :wink: You missed that there are used 2 different types (Atom and String).

Hope it helps.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement