kodepett
Is there a straightforward way of retrieving an element in this list [ {"server", "nginx"}, {"date", "Sat, 13 Nov 2021 18:25:48 GMT"}, {"content-type", "UTF-8"}, {"content-length", "23"}, {"freeflow", "FB"} ]
I’m afraid the ordering might change and hence can’t rely on elem . I’ve perused the doc and can’t find a straightforward of extracting individual fields using the first element(string) in the tuple as the key.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Latest Phoenix Threads
Latest on Elixir Forum
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
thiagomajesk
If you can’t rely on the order I guess you’d have to use
Enum.filterand friends.PS: You could also transform this list into a map and use that map as a lookup.
mpope
If the elements are always tuples of two, I think you can use Keyword.get/3. Something like:
krstfk
Not necessarily the most efficient but should work (not tested) :
It’s also not necessarily the most idiomatic, Enum.find would be better.
Keyword won’t work since the first element of your tuple is a string and not an atom.
josevalim
Unfortunately Keyword expects the keys to be atoms. To lookup for any key, use List.keyfind/3 instead, such as:
The above will look up on a list of tuples returning the first element where the position 0 of the tuple is the string
”server”.kodepett
Awesome, thank you.
krstfk
TIL List.keyfind/3 is backed by a BIF. Nice, thanks.
eksperimental
Filter will return all the elements that have “server” as the first element of the tuple.
eksperimental
Just to illustrate another possibility,
You can use Enum.find/3 and
Enum.find_value/3Enum.find_value is useful since you already know the key, you are probably interested in the value. Anyway I think it is more performant List.keyfind + pattern matching since it is a built-in internal function.
Another comment is that Elixir v1.13 is introducing
List.keyfind!/3you can read more about it here: List — Elixir v1.13.0-rc.0krstfk
I don’t want to hijack this topic, however the fact that
List.keyfind/3is backed by a bif got me curious.I suspect, that this is because there’s no way to implement that function with arbitrary length tuples (should I say n-uples ?), and keys at arbitrary position, in pure Erlang, rather than raw speed considerations.
Benchmarking this leads to surprising results, on my machine with otp 24 with jit enabled:
with
gives the following result :
That’s not to say one should use a custom function in that situation (as always, with performances, one should consider the input size, strike a balance between readability and gains, and benchmark if needed), it was just interesting.
josevalim
You can implement it using regular Elixir: