4 ms·
Possibly stupid question: At the top of the article, we see `(reduce + (map inc (filter even? s)))` -- here, `reduce` is a two-place predicate, taking a functi
by WaxProlix 9y ago
Possibly stupid question:
At the top of the article, we see `(reduce + (map inc (filter even? s)))` -- here, `reduce` is a two-place predicate, taking a function and a collection.
Later, `reduce` is taking a function, a 'base case' [], and a collection -- it's a three-place predicate, more in line with a signature for `fold` than what I'm used to seeing for `reduce`.
Is this a clojure specific overload thing? I'm not at all an expert in FP or anything so it might be fairly standard.
- deleted 9y ago[deleted]
- dragonwriter 9y agoIt's fairly common in functional programming languages for functions to be differentiated both by name and arity (reduce/2 being different from reduce/3.)
- WaxProlix 9y agoI feel like this is fairly opposed to things like partial application and currying in general, no?
- dragonwriter 9y agoIt has implications for the viable syntaxes for partial application and currying, but is not opposed to it. For instance, you could use a distinct sigil for an omitted argument for partial application.
- WaxProlix 9y agoGood point, thanks.
- eliben 9y agoTake a look at https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L6686 https://github.com/clojure/clojure/blob/master/src/clj/cloju... TL;DR: Clojure supports multi-arity functions (sort of like operator overloading in C++, but overloading for the number of operands, not their types) When no default value is given, the result of reduce on the first two elements is used.