mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Reductions as a clojure til.
This commit is contained in:
@@ -13,6 +13,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
- [Expanding Macros](clojure/expanding-macros.md)
|
- [Expanding Macros](clojure/expanding-macros.md)
|
||||||
- [Open JavaDocs](clojure/open-javadocs.md)
|
- [Open JavaDocs](clojure/open-javadocs.md)
|
||||||
- [Quick Clojure Docs](clojure/quick-clojure-docs.md)
|
- [Quick Clojure Docs](clojure/quick-clojure-docs.md)
|
||||||
|
- [Reductions](clojure/reductions.md)
|
||||||
- [Specify the Directory of a Shell Command](clojure/specify-the-directory-of-a-shell-command.md)
|
- [Specify the Directory of a Shell Command](clojure/specify-the-directory-of-a-shell-command.md)
|
||||||
- [Splitting On Whitespace](clojure/splitting-on-whitespace.md)
|
- [Splitting On Whitespace](clojure/splitting-on-whitespace.md)
|
||||||
- [Swap Two Items in a Vector](clojure/swap-two-items-in-a-vector.md)
|
- [Swap Two Items in a Vector](clojure/swap-two-items-in-a-vector.md)
|
||||||
|
|||||||
25
clojure/reductions.md
Normal file
25
clojure/reductions.md
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# Reductions
|
||||||
|
|
||||||
|
One of the core functions in Clojure is
|
||||||
|
[`reduce`](https://clojuredocs.org/clojure.core/reduce). It allows you to
|
||||||
|
build up some result based on applying a function to each value in a
|
||||||
|
collection. Clojure provides a similar function that builds up a (lazy)
|
||||||
|
sequence of intermediate values as it is performing a reduce.
|
||||||
|
This function is
|
||||||
|
[`reductions`](https://clojuredocs.org/clojure.core/reductions).
|
||||||
|
|
||||||
|
Using `reduce` to sum a collection of integers looks like this
|
||||||
|
|
||||||
|
```clojure
|
||||||
|
> (reduce + 0 [1 2 3 4 5])
|
||||||
|
=> 15
|
||||||
|
```
|
||||||
|
|
||||||
|
whereas `reductions` performing the same task will look like this
|
||||||
|
|
||||||
|
```clojure
|
||||||
|
> (reductions + 0 [1 2 3 4 5])
|
||||||
|
=> (0 1 3 6 10 15)
|
||||||
|
```
|
||||||
|
|
||||||
|
h/t Josh Davey
|
||||||
Reference in New Issue
Block a user