Code Correction: reduce

42 views
Skip to first unread message

Drew Verlee

unread,
Sep 16, 2017, 9:17:11 PM9/16/17
to Elements of Clojure
The book seems to claim that reduce will through an exception if the collection its passed only has one argument. This doesn't seem to be the case. As an example:


(reduce (fn [x y] (+ x y)) [1])
=> 1

This seems to work just fine.

The implication is from this text


Typically, when calling reduce we pass in a 2-arity function, like so:

(reduce (fn [x y] (+ x y)) numbers)

However, if numbers has zero arguments in it, reduce will invoke our function with zero arguments. If numbers has one argument in it, reduce will invoke our function with just the first element. In both cases, this will throw an exception. 

Zach Tellman

unread,
Sep 17, 2017, 7:02:54 PM9/17/17
to Drew Verlee, Elements of Clojure
Yes, this is incorrect. I fixed this about a week ago, but it's been waiting on some other changes before I push it out. Thanks for the report.
--
You received this message because you are subscribed to the Google Groups "Elements of Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elements-of-clo...@googlegroups.com.
To post to this group, send email to elements-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elements-of-clojure/f47b77c2-dc3c-436e-870a-252b8cb75159%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages