CastLouis

CastLouis

Hi

i was splitting a string that contains all the keys from a map structure and did a Enum.each to get all the values from the map with map.get after that i joined with Enum.join(“,”)

values_example = String.split(names,",")
                      |> Enum.each(fn(x) -> Map.get(claims,x) end)
                      |> Enum.join(",")

Is it the best way to archive this goal?

Showing Posts 1 to 4

cevado

cevado

Enum.each wont do it. What you want is Enum.map, take a look at the docs:

CastLouis

CastLouis OP

Hi cevado

i was taking your recomendation but, i have a question i change Enum.each for Enum.map and it work, but in the Map.get it returns nil, i don’t know why but i think that is for String.split.

cevado

cevado

It depends on how your map is structured, the keys are atoms or strings? Just read the Map.get documentation to understand it’s behavior.

radar

radar

Using your example here:

claims = %{"first" => "First claim", "second" => "Second claim"}
names = "first,second"

String.split(names,",")
|> Enum.map(fn(x) -> Map.get(claims,x) end)
|> Enum.join(",")

Output: "First claim,Second claim"

This will only work if the keys in claims are strings, as you mentioned earlier. If the keys were not strings but instead were atoms, then you would need convert them, probably doing something like:

keys = for name <- String.split(names,","), do: String.to_atom(name)

(I’m going to assume that they’re strings.)

However, I think this way is cleaner:

keys = names |> String.split(",")
claims |> Map.take(keys) |> Map.values |> Enum.join(",")

Output: "First claim,Second claim"

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews