Merge pull request #1257 from primo-ppcg/any-every

Update `any?`, `every?`
This commit is contained in:
Calvin Rose 2023-08-18 07:20:44 -05:00 committed by GitHub
commit 2ac36a0572
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -1424,20 +1424,21 @@
(fn [& r] (f ;more ;r))))
(defn every?
``Returns true if each value in `ind` is truthy, otherwise returns the first
falsey value.``
``Evaluates to the last element of `ind` if all preceding elements are truthy,
otherwise evaluates to the first falsey argument.``
[ind]
(var res true)
(loop [x :in ind :while res]
(if x nil (set res x)))
(set res x))
res)
(defn any?
``Returns the first truthy value in `ind`, otherwise nil.``
``Evaluates to the last element of `ind` if all preceding elements are falsey,
otherwise evaluates to the first truthy element.``
[ind]
(var res nil)
(loop [x :in ind :until res]
(if x (set res x)))
(set res x))
res)
(defn reverse!

View File

@ -113,13 +113,22 @@
# 7478ad11
(assert (= nil (any? [])) "any? 1")
(assert (= nil (any? [false nil])) "any? 2")
(assert (= nil (any? [nil false])) "any? 3")
(assert (= false (any? [nil false])) "any? 3")
(assert (= 1 (any? [1])) "any? 4")
(assert (nan? (any? [nil math/nan nil])) "any? 5")
(assert (= true
(any? [nil nil false nil nil true nil nil nil nil false :a nil]))
"any? 6")
(assert (= true (every? [])) "every? 1")
(assert (= true (every? [1 true])) "every? 2")
(assert (= 1 (every? [true 1])) "every? 3")
(assert (= nil (every? [nil])) "every? 4")
(assert (= 2 (every? [1 math/nan 2])) "every? 5")
(assert (= false
(every? [1 1 true 1 1 false 1 1 1 1 true :a nil]))
"every? 6")
# Some higher order functions and macros
# 5e2de33
(def my-array @[1 2 3 4 5 6])