silviurosu
I need to convert in place in a map some values only of they exist. The key can be either binary or atom.
Is there a more Elixir way to do it than my solution below?
defp convert_time_params(params) do
params
|> (fn x ->
if Map.has_key?(x, "start_time"), do: update_in(x, ["start_time"], &CalendarUtils.from_iso8601/1), else: x
end).()
|> (fn x ->
if Map.has_key?(x, :start_time),
do: update_in(x, [Access.key!(:start_time)], &CalendarUtils.from_iso8601/1),
else: x
end).()
|> (fn x ->
if Map.has_key?(x, "end_time"), do: update_in(x, ["end_time"], &CalendarUtils.from_iso8601/1), else: x
end).()
|> (fn x ->
if Map.has_key?(x, :end_time),
do: update_in(x, [Access.key!(:end_time)], &CalendarUtils.from_iso8601/1),
else: x
end).()
end
The map is used in Ecto so I can not change the key type from atom to binary. I need to keep the input the same
Trending in Questions
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
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
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
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
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
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
And then
kip
I have a function called deep_map that I use in several different projects to simplify certain classes of map operations. On top of that I have other functions like atomize_keys/3 and so on. It might be useful (or it might be not
)
Summary
Recursively traverse a map and invoke a function
that transforms the map for each key/value pair.
Arguments
mapis anyt:map/0functionis a1-arityfunction or function reference thatis called for each key/value pair of the provided map. It can
also be a 2-tuple of the form
{key_function, value_function}functionis a single function it will becalled with the 2-tuple argument
{key, value}{key_function, value_function}the
key_functionwill be called with the argumentkeyand the valuefunction will be called with the argument
valueoptionsis a keyword list of options. The default is[]Options
:levelindicates the starting (and optionally ending) levels ofthe map at which the
functionis executed. This canbe an integer representing one
levelor a rangeindicating a range of levels. The default is
1..#{@max_level}:onlyis a term or list of terms or acheck function. If it is a termor list of terms, the
functionis only called if thekeyof themap is equal to the term or in the list of terms. If
:onlyis acheck functionthen thecheck functionis passed the{k, v}ofthe current branch in the
map. It is expected to return atruthyvalue that if
truesignals that the argumentfunctionwill be executed.:exceptis a term or list of terms or acheck function. If it is a termor list of terms, the
functionis only called if thekeyof themap is not equal to the term or not in the list of terms. If
:exceptis acheck functionthen thecheck functionis passed the{k, v}ofthe current branch in the
map. It is expected to return atruthyvalue that if
truesignals that the argumentfunctionwill not be executed.Notes
If both the options
:onlyand:exceptare provided then thefunctionis called only when a
termmeets both criteria.Returns
maptransformed by the recursive application offunctionExamples
hauleth
It is very important to point out that this should be used ONLY if you trust source of the binaries. If used on untrusted source then you can experience DoS when malicious party will send data with a lot different strings.
kip
Yes, good to point it out. The function actually has both safe and unsafe modes for exactly that reason. And all my use cases are on CLDR data (and never on user input).