From 7478ad115fde587f9ad13cd12f9a0d9f005d15e9 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 6 Jul 2020 09:19:10 -0500 Subject: [PATCH] Add `any?` predicate to core. This is the contrapositive to `every?`, and is analagous to `or` as `every?` is to `and`. --- src/boot/boot.janet | 9 +++++++++ test/suite0.janet | 20 +++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 27d8e5ab..3fc8bcd0 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -1202,6 +1202,15 @@ (if x nil (set res x))) res) +(defn any? + "Returns the first truthy valye in ind, otherwise nil. + falsey value." + [ind] + (var res nil) + (loop [x :in ind :until res] + (if x (set res x))) + res) + (defn reverse! "Reverses the order of the elements in a given array or buffer and returns it mutated." diff --git a/test/suite0.janet b/test/suite0.janet index fa8352cf..c5ecc226 100644 --- a/test/suite0.janet +++ b/test/suite0.janet @@ -386,17 +386,14 @@ (assert (compare= 3 n3 (table/setproto @{:v 3} mynum)) "compare= poly") (assert (deep= (sorted @[4 5 n3 2] compare<) @[2 n3 4 5]) "polymorphic sort")) -(let [ - MAX_INT_64_STRING "9223372036854775807" +(let [MAX_INT_64_STRING "9223372036854775807" MAX_UINT_64_STRING "18446744073709551615" MAX_INT_IN_DBL_STRING "9007199254740991" NAN (math/log -1) INF (/ 1 0) MINUS_INF (/ -1 0) - compare-poly-tests - [ - [(int/s64 3) (int/u64 3) 0] + [[(int/s64 3) (int/u64 3) 0] [(int/s64 -3) (int/u64 3) -1] [(int/s64 3) (int/u64 2) 1] [(int/s64 3) 3 0] [(int/s64 3) 4 -1] [(int/s64 3) -9 1] @@ -409,11 +406,16 @@ [(+ 1 (int/u64 MAX_INT_IN_DBL_STRING)) (scan-number MAX_INT_IN_DBL_STRING) 1] [(int/s64 0) INF -1] [(int/u64 0) INF -1] [MINUS_INF (int/u64 0) -1] [MINUS_INF (int/s64 0) -1] - [(int/s64 1) NAN 0] [NAN (int/u64 1) 0] - ]] + [(int/s64 1) NAN 0] [NAN (int/u64 1) 0]]] (each [x y c] compare-poly-tests - (assert (= c (compare x y)) (string/format "compare polymorphic %q %q %d" x y c))) - ) + (assert (= c (compare x y)) (string/format "compare polymorphic %q %q %d" x y c)))) + +(assert (= nil (any? [])) "any? 1") +(assert (= nil (any? [false nil])) "any? 2") +(assert (= nil (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") (end-suite)