mathew4509
Iterating through a list
I have a list say
x = ["23gh", "56kh", "97mh"]
I would like to pass each element to Val in each iteration.
Say, in iteration 1 -------- Val = "23gh"
iteration 2 -------- Val = "56kh"
iteration 3 -------- Val = "97mh"
With Enum.at I can pass a specific index but not a set of indices. Any idea how to do it or if there is any other alternative to do this.
Most Liked
pmangalakader
x = ["23gh", "56kh", "97mh"]
for comprehension might help:
for val <- x, do: val
Enum.map implementation:
# Normal
Enum.map(x, fn val ->
val
end)
# Anonymous fn (Shorthand Syntax)
Enum.map(x, &(&1))
If you need to iterate with index, then Enum.with_index might be combined with the above ones.
The official documentation is here:
https://hexdocs.pm/elixir/master/Enum.html
pmangalakader
As I have exactly pointed out, it would be a loop within a loop. Also, Enum.zip/2 requires equal number of elements in both list to have the same number of elements as result. Here the user has to construct the list, which would render another loop to construct it.
Popular in Questions
Other popular topics
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
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









