Hacker News new | ask | show | jobs
by mhanberg 1093 days 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

2 comments

Interesting, 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.

I 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!
I 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.
I generally just use for loops but I’m in the pocket of Big Ignorance.
Elixir doesn't have for loops

    for x <- 1..10 do
      IO.puts x
    end
Isn't that a for loop?
Similar, 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-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...

In Elixir that is a called a comprehension, just like the blog post is discussing.