Permalink
Browse files

New post

  • Loading branch information...
1 parent 1535005 commit 5b83833884eb8bb6243660d273b5b721e3342a6a @djhworld committed Mar 6, 2012
Showing with 7 additions and 8 deletions.
  1. +7 −8 _posts/2012-03-06-reduce-reduce.md
@@ -36,8 +36,8 @@ breaking the problem down and implementing it myself.
;=> 45
{% endhighlight %}
-So in effect, my implementation sets up an *accumulator* (starting at
-zero) and loops through the *sequence* adding the first item on each
+So in effect, my implementation sets up an **accumulator** (starting at
+zero) and loops through the **sequence** adding the first item on each
iteration to the accumulator to eventually yield when
the sequence has been exhausted.
@@ -60,19 +60,18 @@ re-usable
accumulator
(recur (f accumulator (first coll)) (rest coll)))))
- ;=> (consume-sequence + 0 (range 10))
+ ;=> (consume-sequence + 0 (range 10)) ; sum
;=> 45
- ;=> (consume-sequence * 1 [1 2 3 4 5])
+ ;=> (consume-sequence * 1 [1 2 3 4 5]) ; product
;=> 120
{% endhighlight %}
This is in essence what ```reduce``` does. While the underlying
code in Clojure may be different and more efficient, the core
concepts described above are generally the same. So if we translate
-our ```loop```/```recur``` solution for ```sum``` back to reduce
+our ```loop```/```recur``` solution for ```sum``` to ```reduce```
{% highlight clojure %}
- ;(recur (+ accumulator (first coll)) (rest coll)))))
(reduce (fn [accumulator first-item] (+ accumulator first-item)) 0 (range 10))
{% endhighlight %}
@@ -85,8 +84,8 @@ However you'll notice that the above example doesn't entirely match
what we started out with, the higher-order function passed to
```reduce``` seems pretty explicit in its definition. By default,
```reduce``` requires a function that accepts two arguments (the
-*accumulator* and the *first* item of the sequence), so we can
-effectively *reduce* (excuse the pun) the code by just passing the
+**accumulator** and the **first** item of the sequence), so we can
+effectively **reduce** (excuse the pun) the code by just passing the
function name and letting Clojure apply the arguments accordingly.
Seeing as ```+``` accepts multiple arguments, we can just pass that instead.

0 comments on commit 5b83833

Please sign in to comment.