Skip to content

Commit fbfb355

Browse files
committed
filled in some todos in flow control
1 parent 79ca138 commit fbfb355

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

src/cljlab/flowcontrol.clj

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@
3434
;;; Flow control operators are also extensible via macros, which allow the compiler to be extended by user code. We won't be discussing macros today, but you can read more about them at [Clojure.org](http://clojure.org/macros), [Clojure from the Ground Up](https://aphyr.com/posts/305-clojure-from-the-ground-up-macros), or [Clojure for the Brave and True](http://www.braveclojure.com/writing-macros/), among many other places.
3535
;;;
3636
;;; ## `if`
37-
;;; ~\* explanation here \*~
37+
;;;
38+
;;; `if` is the most important conditional expression - it consists of a condition, a "then", and an "else". `if` will only evaluate the branch selected by the conditional.
3839
;; **
3940

40-
;; @@
41-
(ns cljlab.flowcontrol)
42-
;; @@
43-
4441
;; @@
4542
(str "2 is " (if (even? 2) "even" "odd"))
4643
;; @@
@@ -52,7 +49,8 @@
5249

5350
;; **
5451
;;; ## Truthiness
55-
;;; ~\* explanation here \*~
52+
;;;
53+
;;; In Clojure, all values are "truthy" or not. The only "false" values are `false` and `nil` - all other values are "truthy".
5654
;; **
5755

5856
;; @@
@@ -85,7 +83,10 @@
8583

8684
;; **
8785
;;; ## `if` and `do`
88-
;;; ~\* explanation here \*~
86+
;;;
87+
;;; The `if` only takes a single expression for the "then" and "else". Use do to create larger blocks that are a single expression.
88+
;;;
89+
;;; Note that the only reason to do this is if your bodies have side effects! (Why?)
8990
;; **
9091

9192
;; @@
@@ -98,16 +99,21 @@
9899

99100
;; **
100101
;;; ## `when`
101-
;;; ~\* explanation here \*~
102+
;;;
103+
;;; `when` is a one-armed `if`. It checks a condition and then evaluates any number of statements as a body (so no `do` is required). The value of the last expression is returned. If the condition is false, nil is returned.
104+
;;;
105+
;;; `when` communicates to a reader that there is no "else" branch.
102106
;; **
103107

104108
;; @@
105-
;; example here
109+
(when (neg? x)
110+
(throw (RuntimeException. (str "x must be positive: " x))))
106111
;; @@
107112

108113
;; **
109114
;;; ## `cond`
110-
;;; ~\* explanation here \*~
115+
;;;
116+
;;; `cond` is a series of tests and expressions. Each test is evaluated in order and the expression is evaluated and returned for the first true test.
111117
;; **
112118

113119
;; @@
@@ -119,7 +125,8 @@
119125

120126
;; **
121127
;;; ## `cond` and `else`
122-
;;; ~\* explanation here \*~
128+
;;;
129+
;;; If no test is satisfied, nil is returned. A common idiom is to use a final test of `:else`. Keywords (like `:else`) always evaluate to true so this will always be selected as a default.
123130
;; **
124131

125132
;; @@
@@ -132,7 +139,8 @@
132139

133140
;; **
134141
;;; ## `case`
135-
;;; ~\* explanation here \*~
142+
;;;
143+
;;; `case` compares an argument to a series of values to find a match. This is done in constant (not linear) time! However, each value must be a compile-time literal (numbers, strings, keywords, etc).
136144
;; **
137145

138146
;; @@
@@ -146,13 +154,18 @@
146154
(foo 10)
147155
;; @@
148156

157+
;; **
158+
;;; Unlike `cond`, `case` will throw an exception if no value matches:
159+
;; **
160+
149161
;; @@
150162
(foo 11)
151163
;; @@
152164

153165
;; **
154166
;;; ## `case` with else-expression
155-
;;; ~\* explanation here \*~
167+
;;;
168+
;;; `case` can have a final trailing expression that will be evaluated if no test matches.
156169
;; **
157170

158171
;; @@
@@ -163,16 +176,12 @@
163176
"x isn't 5 or 10"))
164177
;; @@
165178

166-
;; @@
167-
(foo 5)
168-
;; @@
169-
170179
;; @@
171180
(foo 10)
172181
;; @@
173182

174183
;; @@
175-
(foo 11)
184+
(foo 11) ;; falls into default instead of erroring
176185
;; @@
177186

178187
;; **
@@ -236,7 +245,7 @@
236245
;;; * Sequences represent iteration as values
237246
;;; * Consumers can partially iterate
238247
;;; * Reducers represent iteration as function composition
239-
;;; * New in Clojure 1.5, not convered here
248+
;;; * Added in Clojure 1.5, not convered here
240249
;;;
241250
;;; ### `loop` and `recur`
242251
;;; * Functional looping construct

0 commit comments

Comments
 (0)