Skip to content

Commit 762db74

Browse files
puredangerstuarthalloway
authored andcommitted
CLJ-1343 - add some?, if-some, and when-some
Signed-off-by: Stuart Halloway <stu@cognitect.com>
1 parent 89dd5d5 commit 762db74

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/clj/clojure/core.clj

+44
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,13 @@
501501
:static true}
502502
[x] (if x false true))
503503

504+
(defn some?
505+
"Returns true if x is not nil, false otherwise."
506+
{:tag Boolean
507+
:added "1.6"
508+
:static true}
509+
[x] (not (nil? x)))
510+
504511
(defn str
505512
"With no args, returns the empty string. With one arg x, returns
506513
x.toString(). (str nil) returns the empty string. With more than
@@ -1746,6 +1753,43 @@
17461753
(let [~form temp#]
17471754
~@body)))))
17481755

1756+
(defmacro if-some
1757+
"bindings => binding-form test
1758+
1759+
If test is not nil, evaluates then with binding-form bound to the
1760+
value of test, if not, yields else"
1761+
{:added "1.6"}
1762+
([bindings then]
1763+
`(if-some ~bindings ~then nil))
1764+
([bindings then else & oldform]
1765+
(assert-args
1766+
(vector? bindings) "a vector for its binding"
1767+
(nil? oldform) "1 or 2 forms after binding vector"
1768+
(= 2 (count bindings)) "exactly 2 forms in binding vector")
1769+
(let [form (bindings 0) tst (bindings 1)]
1770+
`(let [temp# ~tst]
1771+
(if (nil? temp#)
1772+
~else
1773+
(let [~form temp#]
1774+
~then))))))
1775+
1776+
(defmacro when-some
1777+
"bindings => binding-form test
1778+
1779+
When test is not nil, evaluates body with binding-form bound to the
1780+
value of test"
1781+
{:added "1.6"}
1782+
[bindings & body]
1783+
(assert-args
1784+
(vector? bindings) "a vector for its binding"
1785+
(= 2 (count bindings)) "exactly 2 forms in binding vector")
1786+
(let [form (bindings 0) tst (bindings 1)]
1787+
`(let [temp# ~tst]
1788+
(if (nil? temp#)
1789+
nil
1790+
(let [~form temp#]
1791+
~@body)))))
1792+
17491793
(defn push-thread-bindings
17501794
"WARNING: This is a low-level function. Prefer high-level macros like
17511795
binding where ever possible.

test/clojure/test_clojure/control.clj

+15
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@
152152
(exception))
153153
))
154154

155+
(deftest test-if-some
156+
(are [x y] (= x y)
157+
1 (if-some [a 1] a)
158+
false (if-some [a false] a)
159+
nil (if-some [a nil] (exception))
160+
3 (if-some [[a b] [1 2]] (+ a b))
161+
1 (if-some [[a b] nil] b 1)
162+
1 (if-some [a nil] (exception) 1)))
163+
164+
(deftest test-when-some
165+
(are [x y] (= x y)
166+
1 (when-some [a 1] a)
167+
2 (when-some [[a b] [1 2]] b)
168+
false (when-some [a false] a)
169+
nil (when-some [a nil] (exception))))
155170

156171
(deftest test-cond
157172
(are [x y] (= x y)

test/clojure/test_clojure/logic.clj

+7
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,10 @@
203203
; Java objects
204204
(new java.util.Date) ))
205205

206+
(deftest test-some?
207+
(are [expected x] (= expected (some? x))
208+
false nil
209+
true false
210+
true 0
211+
true "abc"
212+
true []))

0 commit comments

Comments
 (0)