The Comprehensive Guide to Elixir's List Comprehension (2022)
mitchellhanberg.com
mitchellhanberg.com
Most important part is they're expressions so you can mix and match them with function calls
Anyway, bookmarking the article because it's the best explainer of this feature I recall and will gladly pass it to others :)
Disclaimer: I am the author of the article and am in the pocket of Big Comprehension
The map approach feels a lot more functional to me and is easy to chain. The `for` method feels a bit awkward. But that said I much prefer Ruby to Python. If I had the opposite preference, I'd probably like the `for` in Elixir more.
for x <- 1..10 do
IO.puts x
end
Isn't that a for loop?https://dev.to/brewinstallbuzzwords/does-elixir-have-for-loo... "Does Elixir have for loops?"
https://elixirforum.com/t/changing-variables-in-a-loop/43526 "Changing variables in a loop"
https://stackoverflow.com/questions/56196664/better-way-writ...
https://stackoverflow.com/questions/48178773/adding-items-to...
Outside of things like generating a game board, or some other well structured n-dimensional struct, I generally avoid nesting them, though. I’ll use Enum on the outer function(s) and filter for the inner one.
They make a ton of sense (way more than they do for Erlang) in the context of EEx, which is part of the standard Elixir distribution, and a first class part of the language
Which are you saying is more functional? And why?
Erlang doesn't have a first class pipe macro or special form I don't think, and its standard library isn't built with this in mind, with consistent data-first argument order. So again it's not "less functional" but it doesn't much use one of the most recognizable idioms of functional programming and the only uniquely functional one many people may be familiar with.
I don't know if this is what they meant but it makes sense to me. In elixir piping streams is an ergonomic and similarly performant solution, with the tradeoffs discussed in the article. There's a lot of use case overlap. If the language didn't have comprehensions it wouldn't be very painful for most people, I don't think. Where erlang without them would be a pain in the ass it really has no other general purpose tool for this.