Fix 'some' (first logical true value or nil)

This commit is contained in:
han semaj 2014-09-04 21:29:38 +12:00
parent c52cff8245
commit 99db02668b
3 changed files with 25 additions and 14 deletions

View File

@ -654,21 +654,25 @@ some
Usage: ``(some pred coll)`` 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. Return the first logical true value of ``(pred x)`` for any ``x`` in
``coll``, otherwise ``nil``. Return ``nil`` if ``coll`` is empty.
.. code-block:: hy .. code-block:: hy
=> (some even? [2 4 6]) => (some even? [2 4 6])
True True
=> (some even? [1 3 5]) => (nil? (some even? [1 3 5]))
False
=> (some even? [1 3 6])
True True
=> (some even? []) => (nil? (some identity [0 "" []]))
False True
=> (some identity [0 "non-empty-string" []])
'non-empty-string'
=> (nil? (some even? []))
True
.. _string?-fn: .. _string?-fn:

View File

@ -26,7 +26,7 @@
(import itertools) (import itertools)
(import functools) (import functools)
(import collections) (import collections)
(import sys) (import sys)
(import [hy._compat [long-type]]) ; long for python2, int for python3 (import [hy._compat [long-type]]) ; long for python2, int for python3
(import [hy.models.cons [HyCons]]) (import [hy.models.cons [HyCons]])
(import [hy.lex [LexException PrematureEndOfInput tokenize]]) (import [hy.lex [LexException PrematureEndOfInput tokenize]])
@ -299,8 +299,8 @@
(nth coll 1)) (nth coll 1))
(defn some [pred coll] (defn some [pred coll]
"Return true if (pred x) is logical true for any x in coll, else false" "Return the first logical true value of (pred x) for any x in coll, else nil"
(any (map pred coll))) (first (filter nil (map pred coll))))
(defn string [x] (defn string [x]
"Cast x as current string implementation" "Cast x as current string implementation"
@ -347,7 +347,7 @@
(setv buff (+ buff inn)) (setv buff (+ buff inn))
(try (try
(def parsed (first (tokenize buff))) (def parsed (first (tokenize buff)))
(except [e [LexException PrematureEndOfInput IndexError]]) (except [e [LexException PrematureEndOfInput IndexError]])
(else (if parsed (break))))) (else (if parsed (break)))))
parsed) parsed)

View File

@ -474,9 +474,16 @@
(defn test-some [] (defn test-some []
"NATIVE: testing the some function" "NATIVE: testing the some function"
(assert-true (some even? [2 4 6])) (assert-true (some even? [2 4 6]))
(assert-false (some even? [1 3 5])) (assert-nil (some even? [1 3 5]))
(assert-true (some even? [1 3 6])) (assert-true (some even? [1 2 3]))
(assert-false (some even? []))) (assert-nil (some even? []))
; 0, "" (empty string) and [] (empty list) are all logical false
(assert-nil (some identity [0 "" []]))
; non-empty string is logical true
(assert-equal (some identity [0 "this string is non-empty" []])
"this string is non-empty")
; nil if collection is empty
(assert-nil (some even? [])))
(defn test-string? [] (defn test-string? []
"NATIVE: testing string?" "NATIVE: testing string?"