PJUllrich
Author of Building Table Views with Phoenix LiveView
Idiomatic way of handling missing Map fields
I’m extracting many fields from a map using pattern-matching against the function parameter. Sometimes only a single field is missing from the input map. I’m handling this case with Map.merge-ing a default value into the input map and calling the same function again.
However, this feels not quite idiomatic. Can you maybe think of a more idiomatic way of handling this case?
Example:
defmodule Foobar do
def foo(%{"a" => a, "b" => b, "c" => c, "d" => d, "e" => e}), do: do_something(a, b, c, d, e)
def foo(params), do: foo(Map.merge(params, %{"e" => 10}))
def do_something(a, b, c, d, e), do: IO.inspect("#{a}-#{b}-#{c}-#{d}-#{e}")
end
Foobar.foo(%{"a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5})
Foobar.foo(%{"a" => 1, "b" => 2, "c" => 3, "d" => 4})
```
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 8 of 8 Posts
sigu
What about defining a struct with default values, then using struct kernel function to map the values
Marcus
@sigu
This case should not be the cause of using a struct. I mean the decision for a struct should be related to the problem and not to using some special syntax.
@PJUllrich
The idiomatic way for me is not to use pattern matching just for the case of extracting values from a map. The pattern matching in the parameters should have always a good reason.
For me
foocould be written as:or
dimitarvp
@Marcus gives you a good way to go about it. I’ll offer one more:
brettbeatty
I don’t think it’s bad to use
Map.merge/2, but the way you use it as a fallback seems weird (and you wouldn’t have a base case if your map didn’t have an"a"key, for example).Here’s how I would write your module:
But that may just be because of how I handle options to functions. I write a lot of single-public-function modules where I’m passing around the options to a lot of private functions and want the defaulting to happen in one place. They’ll look something like this:
ericgray
Is the missing field always the same, or does it vary? If the missing field is always the same then of course you’ll only need one
default value.I’m assuming the missing field can vary, in that case the way you wrote your code calls for a lot of pattern matching.
Not sure how you need to use your data in the end but assuming the map fields are known upfront I wrote it like this without the formatting.
PJUllrich
Yes, the missing field is always the same.
Thanks a lot for your solution, however it feels a bit overkill
But it would be a good solution if any of the fields could be missing.
PJUllrich
Hmm interesting! I like the second solution especially since it’s easily extendible if another field would be missing as well. Thanks!
PJUllrich
I agree with @Marcus. I’d like to keep on using a map instead of having to define a struct for this function only. But if this case would be occurring in more than one function, the struct would actually be the way to go I think, so thanks