3 ms·
That quote is hilarious -- Guido von Rossum claims that it's impossible to implement `reduce` in a few lines in a functional language. Just out of curiosity, I
by lbarrow 8y ago
That quote is hilarious -- Guido von Rossum claims that it's impossible to implement `reduce` in a few lines in a functional language. Just out of curiosity, I checked out the `reduce` implementation in Elixir:
def reduce(enumerable, fun) do
result =
Enumerable.reduce(enumerable, {:cont, :first}, fn
x, :first -> {:cont, {:acc, x}}
x, {:acc, acc} -> {:cont, {:acc, fun.(x, acc)}}
end)
|> elem(1)
case result do
:first -> raise Enum.EmptyError
{:acc, acc} -> acc
end
end
Seems pretty straightforward to me.
https://github.com/elixir-lang/elixir/blob/v1.7.4/lib/elixir/lib/enum.ex#L1872-L1884 https://github.com/elixir-lang/elixir/blob/v1.7.4/lib/elixir...
- darkpuma 8y agoExactly. In scheme reduce-left/fold-left can be as simple as: (define (reduce-left fn init lst) (if (null? lst) init (reduce-left fn (fn init (car lst)) (cdr lst)))) It's clear from that quote that Guido (at least at that time) doesn't have real experience with functional programming and is letting his personal biases cloud his judgement. Okay maybe '"scheme isn't a functional language because it allows side effects and only Haskell and family are allowed to call themselves functional", or whatever. He seems to perhaps hint at that mentality. But the fact remains that a language like scheme makes this much easier than python.
- kbp 8y agoWhat do you mean by the last paragraph? foldl in Haskell is just foldl _ acc [] = acc foldl f acc (x:xs) = foldl f (f acc x) xs And in Python something like def foldl(f, acc, l): for x in l: acc = f(acc, x) return acc Which doesn't seem much harder to read or write than any other version, to me (it's all just syntax around the same algorithm).