1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 09:17:17 +00:00

update any?, every?

Updates `any?` and `every?` to be exact functional analogues to `or` and `and`.
This commit is contained in:
primo-ppcg 2023-08-18 07:39:30 +07:00
parent 43a6a70e1e
commit 15760b0950
2 changed files with 16 additions and 6 deletions

View File

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

View File

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