Comment

Comment by dnautics

this seems to be a syntax problem. The for loop is not really 'equivalent' in complexity to the functional solution. In Elixir, I'd do this:

   list
   |> Enum.reduce(0, fn ->
      x, acc when mod(x, 3) == 0 -> acc + x * x
      _, acc -> acc
   end)
But the presented functional solution throws more functions at it, which might look like this, which I think is quite clear.

    list
    |> Enum.filter(&(mod(&1, 3) == 0))
    |> Enum.map(&(&1 * &1))
    |> Enum.reduce(acc, &Kernel.+/2)  #better yet use Enum.sum
parent dnautics 6 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton