jeramyRR
I have a list that only has two values in it, and I want to place the elements in a map. Is there an easy way to deconstruct the list so that I can get the last value without it being a list itself?
Here’s what I’m currently doing:
def split_metadata(metadata) do
key_value_pairs =
metadata
|> String.split(:binary.compile_pattern([":", ";"]), trim: true)
|> Stream.chunk_every(2, 2, :discard)
|> Enum.reduce(%{}, fn [key | [value | []]], acc -> Map.put(acc, key, value) end)
end
iex > "build:1.2;branch:git_something_develop;branchname:weeble;key:value;woot:" |> Artifact.split_metadata()
%{
"branch" => "git_something_develop",
"branchname" => "weeble",
"build" => "1.2",
"key" => "value"
}
That works just fine, but it’s kind of ugly, the [ key | [value | []]] part that is.
I know there’s a List.first and a List.last, but that’s ugly too.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
josevalim
[key, value]is equivalent to[key | [value | []]]. In other words:jeramyRR
Doh, I see what I’m doing wrong. I’m doing [ key | value ] instead of [ key , value ].
voltone
Also, you could use
Map.new(&List.to_tuple/1)instead of yourreducecall, it does effectively the same thing.jeramyRR
Nice, thanks for the tip!
peerreynders
Some unsolicited remarks.
Apparently this isn’t unusual:
Without too much effort I found another, similar example:
Elixir Convert Query String Parameters into Map (Key/Value Pairs) · GitHub
Given the context this approach may be reasonable.
However on first blush that transformation would set a red flag for me on review. If I had to put it into words I would say:
Effectively both “
:” and “;” are treated as delimiters with identical meaning which isn’t the intention of the format. The test case didn’t make me feel any better. Without much imagination it could be rewritten as:and the code as implemented would return:
Fine.
But for the sake of argument: valueless keys aren’t unheard of (e.g.
disabled,readonly,requiredattributes in aninputHTML tag). For the moment lets assume that we want to keep those keys - for example:or when discarding valueless keys:
In my opinion both of these examples take (better) advantage of the natural, implied shape of the data. The other approach feels more like “smashing everything to bits and then picking up the pieces”.
jeramyRR
@peerreynders, thanks for unsolicited advice. You were definitely right here. I was ignoring the intended shape of the data being passed in and was just smashing it all together. I’ve re-written it as follows:
peerreynders
Yikes. I didn’t actually expect you to change anything - I just wanted to present my personal impression. Now I’m wondering whether I pushed you too far.
Again, my personal take for “consideration”:
As per style guide would be written as
or
which raises the question: does this function provide any value beyond
Map.new/1? I personally have no qualms about single line functions as long as the name is more clear than than a cryptic one liner.If your sample string has a representative length then
Streammight be overkill here.Pipelining is useful but it can sometimes lead to fragmentation of logic. This way may be crystal clear to you and then it’s fine.
I would lean towards more functions and less pipes. A grossly exaggerated example:
It’s a balancing act.
Further
I don’t think the branching is adding any value. This code should focus on separating the “key value candidates” from one another. Whether or not any particular candidate is a key value should be resolved separately.