4 ms·
I always use `or` because it's IMO most readable: (or x 5) ;;=> returns x if x is not nil, else 5 E.g., (-> {:a 1 :b 2} :c (or 5.5)
by dmichulke 8y ago
I always use `or` because it's IMO most readable:
(or x 5) ;;=> returns x if x is not nil, else 5
E.g.,
(-> {:a 1 :b 2}
:c
(or 5.5)
inc)
- delish 8y ago> ;;=> returns x if x is nil, else 5 Should this be: > ;;=> returns x if x is not nil, else 5
- kimi 8y agoFranky, this looks great and should be in the article :-)
- serpix 8y agoyes! you have eliminated repeating yourself and returning nil. Other ways to do this very simple example: `(some-> m :c (inc 5))` there is no need to defend against nil values. Hickey has your back on built in functions.
- lkitching 8y agoOne problem with using or is if the key is associated with false, then it will be ignored in favour of the default. Keywords support a second argument which is the default to use on not-found so you can just write: (-> {:a 1 :b 2} (:c 5.5) inc)