Skip to content

Commit f6335da

Browse files
committed
Add for reduce example and copy content on it from API doc side
1 parent 46554ea commit f6335da

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

content/documentation/control-flow.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ have the form `:modifier argument`. The following modifiers are supported:
130130
* `:while` breaks the loop if the expression is falsy.
131131
* `:let` defines additional bindings.
132132
* `:when` only evaluates the loop body if the condition is true.
133+
* `:reduce [accumulator initial-value]` Instead of returning a list, it reduces the values into `accumulator`. Initially `accumulator` is bound to `initial-value`.
133134

134135
```phel
135136
(for [x :range [0 3]] x) # Evaluates to [0 1 2]
@@ -143,6 +144,8 @@ have the form `:modifier argument`. The following modifiers are supported:
143144
144145
(for [[k v] :pairs {:a 1 :b 2 :c 3}] [v k]) # Evaluates to [[1 :a] [2 :b] [3 :c]]
145146
(for [[k v] :pairs [1 2 3]] [k v]) # Evaluates to [[0 1] [1 2] [2 3]]
147+
(for [[k v] :pairs {:a 1 :b 2 :c 3} :reduce [m {}]]
148+
(put m k (inc v))) # Evaluates to {:a 2 :b 3 :c 4}
146149
147150
(for [x :in [2 2 2 3 3 4 5 6 6] :while (even? x)] x) # Evaluates to [2 2 2]
148151
(for [x :in [2 2 2 3 3 4 5 6 6] :when (even? x)] x) # Evaluates to [2 2 2 4 6 6]

0 commit comments

Comments
 (0)