4 ms·
I generally default to a comprehension unless it becomes illegible. Disclaimer: I am the author of the article and am in the pocket of Big Comprehension
by mhanberg 3y ago
I generally default to a comprehension unless it becomes illegible.
Disclaimer: I am the author of the article and am in the pocket of Big Comprehension
- freedomben 3y agoInteresting, I use `map` all the time and `for` rarely. I'm not necessarily disagreeing with you as I see this as more of a style preference. 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.
- square_usual 3y agoI agree with you: I prefer map and reduce unless I specifically need a comprehension because of multiple input collections. IMO for with many conditions etc. becomes too hard to understand, and you have to keep too many things in mind vs. a filter |> map. Regardless, I don't mind its presence, and it definitely has come in handy at times. Yet another reason to enjoy Elixir's approach to language design!
- bluesnowmonkey 3y agoI also use ‘for’ rarely. It silently ignore list elements that don’t match, whereas ‘map’ or ‘each’ blow up. Generally I prefer things to blow up rather than fail silently.
- nickpeterson 3y agoI generally just use for loops but I’m in the pocket of Big Ignorance.
- PKop 3y agoElixir doesn't have for loops
- arrowsmith 3y agofor x <- 1..10 do IO.puts x end Isn't that a for loop?
- Munksgaard 3y agoIn Elixir that is a called a comprehension, just like the blog post is discussing.
- PKop 3y agoSimilar, but different in subtle ways when you get beyond the toy example posted here...particularly that it's always iterating a collection, vs being able to just arbitrarily loop based on arbitrary conditions..where you'd be able to easily break out of it, modify a mutable collection within it etc https://dev.to/brewinstallbuzzwords/does-elixir-have-for-loops-19p9 https://dev.to/brewinstallbuzzwords/does-elixir-have-for-loo... "Does Elixir have for loops?" https://elixirforum.com/t/changing-variables-in-a-loop/43526 https://elixirforum.com/t/changing-variables-in-a-loop/43526 "Changing variables in a loop" https://stackoverflow.com/questions/56196664/better-way-writing-nested-for-loops-in-elixir https://stackoverflow.com/questions/56196664/better-way-writ... https://stackoverflow.com/questions/48178773/adding-items-to-a-list-from-a-loop-with-conditions https://stackoverflow.com/questions/48178773/adding-items-to...