‹ BackHN Continuity

Thread

Anecdotally, programmers dislike "reduce"

175 points · 283 comments · vinhnx

  1. Terr_ · · focus · HN ↗
    Sure, I'm up for some bike-shedding. [0][1] Unless performance demands otherwise, I prefer map+filter because:

    1. It's cheaper/faster at communicating intent to humans reading your code. Since a reduce call can do all sorts of interesting things, people need to stare harder to realize "oh, it's just doing a a map and filter together."

    2. Things are easier to debug. I can vet the process of transformation (and its intermediate results) and then vet the process of excluding some of those results.

    _____

    With respect to debugging, a sample form Elixir's REPL where the piping (|>) to the dbg() function reveals the intermediate state:

        iex(1)> [5,34,6,2,7,3,1] |> 
                         Enum.map(fn x -> x * x end) |>
                         Enum.filter(fn x -> x < 10 end) |> 
                         dbg()
    
        [iex:4: (file)]
        [5, 34, 6, 2, 7, 3, 1] #=> [5, 34, 6, 2, 7, 3, 1]
        |> Enum.map(fn x -> x * x end) #=> [25, 1156, 36, 4, 49, 9, 1]
        |> Enum.filter(fn x -> x < 10 end) #=> [4, 9, 1]
    
    
    
    [0] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Law_of_triviality" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Law_of_triviality

    [1] <a href="https:&#x2F;&#x2F;www.smbc-comics.com&#x2F;comic&#x2F;noun" rel="nofollow">https:&#x2F;&#x2F;www.smbc-comics.com&#x2F;comic&#x2F;noun

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.