You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/documentation/control-flow.md
+27-15Lines changed: 27 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,12 +14,12 @@ A control flow structure. First evaluates _test_. If _test_ evaluates to `true`,
14
14
The _test_ evaluates to `false` if its value is `false` or equal to `nil`. Every other value evaluates to `true`. In sense of PHP this means (`test != null && test !== false`).
15
15
16
16
```phel
17
-
(if true 10) # evaluates to 10
18
-
(if false 10) # evaluates to nil
19
-
(if true (print 1) (print 2)) # prints 1 but not 2
20
-
(if 0 (print 1) (print 2)) # prints 2
21
-
(if nil (print 1) (print 2)) # prints 2
22
-
(if [] (print 1) (print 2)) # prints 2
17
+
(if true 10) # Evaluates to 10
18
+
(if false 10) # Evaluates to nil
19
+
(if true (print 1) (print 2)) # Prints 1 but not 2
20
+
(if 0 (print 1) (print 2)) # Prints 2
21
+
(if nil (print 1) (print 2)) # Prints 2
22
+
(if [] (print 1) (print 2)) # Prints 2
23
23
```
24
24
25
25
## Case
@@ -95,20 +95,20 @@ Internally `recur` is implemented as a PHP while loop and therefore prevents the
95
95
(foreach [value valueExpr] expr*)
96
96
(foreach [key value valueExpr] expr*)
97
97
```
98
-
The `foreach` special form can be used to iterate over all kind of PHP datastructures. The return value of `foreach` is always `nil`. The `loop` special form should be preferred of the `foreach` special form whenever possible.
98
+
The `foreach` special form can be used to iterate over all kind of PHP datastructures for side-effects. The return value of `foreach` is always `nil`. The `loop` special form should be preferred of the `foreach` special form whenever possible.
99
99
100
100
```phel
101
101
(foreach [v [1 2 3]]
102
-
(print v)) # prints 1, 2 and 3
102
+
(print v)) # Prints 1, 2 and 3
103
103
104
104
(foreach [k v {"a" 1 "b" 2}]
105
105
(print k)
106
-
(print v)) # prints "a", 1, "b" and 2
106
+
(print v)) # Prints "a", 1, "b" and 2
107
107
```
108
108
109
109
## For
110
110
111
-
A more powerful loop functionality is provided by the `for` loop. The `for` loop is an elegant way to define and create arrays based on existing collections. It combines the functionality of `foreach`, `let`and `if` in one call.
111
+
A more powerful loop functionality is provided by the `for` loop. The `for` loop is an elegant way to define and create arrays based on existing collections. It combines the functionality of `foreach`, `let`, `if`and `reduce` in one call.
112
112
113
113
```phel
114
114
(for head body+)
@@ -130,6 +130,7 @@ have the form `:modifier argument`. The following modifiers are supported:
130
130
*`:while` breaks the loop if the expression is falsy.
131
131
*`:let` defines additional bindings.
132
132
*`: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`.
133
134
134
135
```phel
135
136
(for [x :range [0 3]] x) # Evaluates to [0 1 2]
@@ -143,15 +144,26 @@ have the form `:modifier argument`. The following modifiers are supported:
(dofor [x :in [1 2 3]] (print x)) # Prints 1, 2, 3 and returns nil
162
+
(dofor [x :in [2 3 4 5] :when (even? x)] (print x)) # Prints 1, 2 and returns nil
163
+
```
164
+
165
+
Iterating over collections for side-effects is also possible with `dofor` which has similar behavior to `for` otherwise but returns `nil` as `foreach` does.
166
+
155
167
## Exceptions
156
168
157
169
```phel
@@ -169,11 +181,11 @@ The _expr_ is evaluated and thrown, therefore _expr_ must return a value that im
169
181
All expressions are evaluated and if no exception is thrown the value of the last expression is returned. If an exception occurs and a matching _catch-clause_ is provided, its expression is evaluated and the value is returned. If no matching _catch-clause_ can be found the exception is propagated out of the function. Before returning normally or abnormally the optionally _finally-clause_ is evaluated.
170
182
171
183
```phel
172
-
(try) # evaluates to nil
184
+
(try) # Evaluates to nil
173
185
174
186
(try
175
187
(throw (php/new \Exception))
176
-
(catch \Exception e "error")) # evaluates to "error"
188
+
(catch \Exception e "error")) # Evaluates to "error"
177
189
178
190
(try
179
191
(+ 1 1)
@@ -182,7 +194,7 @@ All expressions are evaluated and if no exception is thrown the value of the las
182
194
(try
183
195
(throw (php/new \Exception))
184
196
(catch \Exception e "error")
185
-
(finally (print "test"))) # evaluates to "error" and prints "test"
197
+
(finally (print "test"))) # Evaluates to "error" and prints "test"
0 commit comments