Implement every? and some
This commit is contained in:
parent
fed3ec302c
commit
24a1567b00
@ -138,6 +138,32 @@ Return True if ``coll`` is empty, i.e. ``(= 0 (len coll))``.
|
|||||||
False
|
False
|
||||||
|
|
||||||
|
|
||||||
|
.. _every?-fn:
|
||||||
|
|
||||||
|
every?
|
||||||
|
------
|
||||||
|
|
||||||
|
.. versionadded:: 0.9.13
|
||||||
|
|
||||||
|
Usage: ``(every? pred coll)``
|
||||||
|
|
||||||
|
Return True if ``(pred x)`` is logical true for every ``x`` in ``coll``, otherwise False. Return True if ``coll`` is empty.
|
||||||
|
|
||||||
|
.. code-block:: clojure
|
||||||
|
|
||||||
|
=> (every? even? [2 4 6])
|
||||||
|
True
|
||||||
|
|
||||||
|
=> (every? even? [1 3 5])
|
||||||
|
False
|
||||||
|
|
||||||
|
=> (every? even? [2 4 5])
|
||||||
|
False
|
||||||
|
|
||||||
|
=> (every? even? [])
|
||||||
|
True
|
||||||
|
|
||||||
|
|
||||||
.. _float?-fn:
|
.. _float?-fn:
|
||||||
|
|
||||||
float?
|
float?
|
||||||
@ -570,6 +596,32 @@ Return the second member of ``coll``. Equivalent to
|
|||||||
1
|
1
|
||||||
|
|
||||||
|
|
||||||
|
.. _some-fn:
|
||||||
|
|
||||||
|
some
|
||||||
|
----
|
||||||
|
|
||||||
|
.. versionadded:: 0.9.13
|
||||||
|
|
||||||
|
Usage: ``(some pred coll)``
|
||||||
|
|
||||||
|
Return True if ``(pred x)`` is logical true for any ``x`` in ``coll``, otherwise False. Return False if ``coll`` is empty.
|
||||||
|
|
||||||
|
.. code-block:: clojure
|
||||||
|
|
||||||
|
=> (some even? [2 4 6])
|
||||||
|
True
|
||||||
|
|
||||||
|
=> (some even? [1 3 5])
|
||||||
|
False
|
||||||
|
|
||||||
|
=> (some even? [1 3 6])
|
||||||
|
True
|
||||||
|
|
||||||
|
=> (some even? [])
|
||||||
|
False
|
||||||
|
|
||||||
|
|
||||||
.. _string?-fn:
|
.. _string?-fn:
|
||||||
|
|
||||||
string?
|
string?
|
||||||
|
@ -108,6 +108,10 @@
|
|||||||
(_numeric-check n)
|
(_numeric-check n)
|
||||||
(= (% n 2) 0))
|
(= (% n 2) 0))
|
||||||
|
|
||||||
|
(defn every? [pred coll]
|
||||||
|
"Return true if (pred x) is logical true for every x in coll, else false"
|
||||||
|
(all (map pred coll)))
|
||||||
|
|
||||||
(defn fake-source-positions [tree]
|
(defn fake-source-positions [tree]
|
||||||
"Fake the source positions for a given tree"
|
"Fake the source positions for a given tree"
|
||||||
(if (and (iterable? tree) (not (string? tree)))
|
(if (and (iterable? tree) (not (string? tree)))
|
||||||
@ -294,6 +298,10 @@
|
|||||||
"Return second item from `coll`"
|
"Return second item from `coll`"
|
||||||
(get coll 1))
|
(get coll 1))
|
||||||
|
|
||||||
|
(defn some [pred coll]
|
||||||
|
"Return true if (pred x) is logical true for any x in coll, else false"
|
||||||
|
(any (map pred coll)))
|
||||||
|
|
||||||
(defn string [x]
|
(defn string [x]
|
||||||
"Cast x as current string implementation"
|
"Cast x as current string implementation"
|
||||||
(if-python2
|
(if-python2
|
||||||
@ -338,9 +346,9 @@
|
|||||||
(= n 0))
|
(= n 0))
|
||||||
|
|
||||||
(def *exports* '[calling-module-name coll? cons cons? cycle dec distinct
|
(def *exports* '[calling-module-name coll? cons cons? cycle dec distinct
|
||||||
disassemble drop drop-while empty? even? first filter
|
disassemble drop drop-while empty? even? every? first filter
|
||||||
flatten float? gensym identity inc instance? integer
|
flatten float? gensym identity inc instance? integer
|
||||||
integer? integer-char? iterable? iterate iterator?
|
integer? integer-char? iterable? iterate iterator?
|
||||||
list* macroexpand macroexpand-1 neg? nil? none? nth
|
list* macroexpand macroexpand-1 neg? nil? none? nth
|
||||||
numeric? odd? pos? remove repeat repeatedly rest second
|
numeric? odd? pos? remove repeat repeatedly rest second
|
||||||
string string? take take-nth take-while zero?])
|
some string string? take take-nth take-while zero?])
|
||||||
|
@ -123,6 +123,13 @@
|
|||||||
(try (even? None)
|
(try (even? None)
|
||||||
(catch [e [TypeError]] (assert (in "not a number" (str e))))))
|
(catch [e [TypeError]] (assert (in "not a number" (str e))))))
|
||||||
|
|
||||||
|
(defn test-every? []
|
||||||
|
"NATIVE: testing the every? function"
|
||||||
|
(assert-true (every? even? [2 4 6]))
|
||||||
|
(assert-false (every? even? [1 3 5]))
|
||||||
|
(assert-false (every? even? [2 4 5]))
|
||||||
|
(assert-true (every? even? [])))
|
||||||
|
|
||||||
(defn test-filter []
|
(defn test-filter []
|
||||||
"NATIVE: testing the filter function"
|
"NATIVE: testing the filter function"
|
||||||
(setv res (list (filter pos? [ 1 2 3 -4 5])))
|
(setv res (list (filter pos? [ 1 2 3 -4 5])))
|
||||||
@ -399,6 +406,13 @@
|
|||||||
(assert-equal 2 (second [1 2]))
|
(assert-equal 2 (second [1 2]))
|
||||||
(assert-equal 3 (second [2 3 4])))
|
(assert-equal 3 (second [2 3 4])))
|
||||||
|
|
||||||
|
(defn test-some []
|
||||||
|
"NATIVE: testing the some function"
|
||||||
|
(assert-true (some even? [2 4 6]))
|
||||||
|
(assert-false (some even? [1 3 5]))
|
||||||
|
(assert-true (some even? [1 3 6]))
|
||||||
|
(assert-false (some even? [])))
|
||||||
|
|
||||||
(defn test-string? []
|
(defn test-string? []
|
||||||
"NATIVE: testing string?"
|
"NATIVE: testing string?"
|
||||||
(assert-true (string? "foo"))
|
(assert-true (string? "foo"))
|
||||||
@ -456,4 +470,3 @@
|
|||||||
(assert-equal res [None None])
|
(assert-equal res [None None])
|
||||||
(setv res (list (take-while (fn [x] (not (none? x))) [1 2 3 4 None 5 6 None 7])))
|
(setv res (list (take-while (fn [x] (not (none? x))) [1 2 3 4 None 5 6 None 7])))
|
||||||
(assert-equal res [1 2 3 4]))
|
(assert-equal res [1 2 3 4]))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user