billposters
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.
Trending in Questions
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
Hi all!
I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Hey, I know this has been discussed ad nauseum, but I couldn’t find a definitive answer or at least some consensus around this, so let me...
New
I don’t suppose you can lazy load a library depending on runtime data? For example if it’s a certain user they can lazy load ‘library A’ ...
New
Other Trending Topics
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
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
- #code-sync
- #exunit
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #blog-post
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
brettbeatty
I would actually swap the roles of your
lookupandparamsvariables. If order doesn’t matter, you could just turnlookupinto a map and iterate overparamsto create your expectedoutput.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:
Have not tested it, but I’d believe it will work
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 toiforcaseon a check for its existenceEiji
You have 2 mistakes in code:
You are calling
params.kexpecting thatkis variable.params.kworks likeparams[:k]and therefore it would not work for you. Also you cannot mixAtomandStringtypes as they are completely different types.Your
withclause would alwaysraisesomething like:The raison is described in 1st point. However mistake here is to assume that
raisewould simply failwithclause.Elixirway is “let it fail”. Of course you can writecatch/try, but unless you have a strong reason for it it’s generally not recommend.Here are my 2 proposals:
There are 2 major differences between
safeandunsafeversions:safeis good for data from untrusted source like user inputunsafeis faster because we have instead ofEnum.find/2we are using purepattern matchingwhich is optimized by compilerIn case we are sure about existence of
string_keyanywhere inparams(asAtom) we can useString.to_existing_atom/1which is safe, but in case like yours it would raiseArgumentErrorand that’s why I have not provided asafeway with optimizedpattern-matchingusage.Code you wrote is good in theory and may be bad in practice.
You are doing something (swapping roles of 2 variables) without a discussion with author of topic. This is called
assumptionand 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:On
proddata - 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 inprod.Even if assumption works in practice it may fail when
enhancingoriginal theory. Let’s take an example. Somebody asked you to make a decorations and you should useeggsfor it. Mostly by assumption ofeasterdaypainting eggs is just “normal”, but if we do not want them foreasterdayeven if they would be worth thousands ofUSDwe may need new eggs for decorations.This one is unfortunately a popular mistake. Using
[head | tail]optimization along withEnum.reverse/1is faster than appending two lists like in your example.You see well, but you do not everything.
You missed that there are used 2 different types (
AtomandString).Hope it helps.
kokolegorille
My ugly one.. I’m also a fan of looping over params, and using lookup as a dictionary.
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.
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.