if statement - When to use "if" and "when" in Clojure? -
when 1 better other? 1 faster other or difference return of false or nil?
use if
when have 1 truthy , 1 falsy case , don't need implicit do
block. in contrast, when
should used when have handle truthy case , implicit do
. there no difference in speed, it's matter of using idiomatic style.
(if (my-predicate? my-data) (do-something my-data) (do-something-else my-data)) (when (my-predicate? my-data) (do-something my-data) (do-something-additionally my-data))
in if
case, do-something
run if my-predicate?
returns truthy result, whereas in when
case, both do-something
, do-something-additionally
executed.
Comments
Post a Comment