diff --git a/docs/language/core.rst b/docs/language/core.rst index 4aa2f53..59af6bc 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -654,21 +654,25 @@ some 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 => (some even? [2 4 6]) True - => (some even? [1 3 5]) - False - - => (some even? [1 3 6]) + => (nil? (some even? [1 3 5])) True - => (some even? []) - False + => (nil? (some identity [0 "" []])) + True + + => (some identity [0 "non-empty-string" []]) + 'non-empty-string' + + => (nil? (some even? [])) + True .. _string?-fn: diff --git a/hy/core/language.hy b/hy/core/language.hy index bc5587a..e81d8cd 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -26,7 +26,7 @@ (import itertools) (import functools) (import collections) -(import sys) +(import sys) (import [hy._compat [long-type]]) ; long for python2, int for python3 (import [hy.models.cons [HyCons]]) (import [hy.lex [LexException PrematureEndOfInput tokenize]]) @@ -299,8 +299,8 @@ (nth 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))) + "Return the first logical true value of (pred x) for any x in coll, else nil" + (first (filter nil (map pred coll)))) (defn string [x] "Cast x as current string implementation" @@ -347,7 +347,7 @@ (setv buff (+ buff inn)) (try (def parsed (first (tokenize buff))) - (except [e [LexException PrematureEndOfInput IndexError]]) + (except [e [LexException PrematureEndOfInput IndexError]]) (else (if parsed (break))))) parsed) diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index 2f858ce..3e390a6 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -474,9 +474,16 @@ (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? []))) + (assert-nil (some even? [1 3 5])) + (assert-true (some even? [1 2 3])) + (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? [] "NATIVE: testing string?"