Merge branch 'f/is-coll' of https://github.com/theanalyst/hy into theanalyst-f/is-coll

This commit is contained in:
Nicolas Dandrimont 2014-01-12 17:33:17 +01:00
commit 14a791f37b
3 changed files with 38 additions and 3 deletions

View File

@ -6,6 +6,29 @@ Hy Core
Core Functions
===============
.. _is-coll-fn:
coll?
----
.. versionadded:: 0.9.13
Usage: ``(coll? x)``
Returns true if argument is iterable and not a string.
.. code-block:: clojure
=> (coll? [1 2 3 4])
True
=> (coll? {"a" 1 "b" 2})
True
=> (coll? "abc")
False
.. _dec-fn:
dec

View File

@ -30,6 +30,10 @@
(if (not (numeric? x))
(raise (TypeError (.format "{0!r} is not a number" x)))))
(defn coll? [coll]
"Checks whether item is a collection"
(and (iterable? coll) (not (string? coll))))
(defn cycle [coll]
"Yield an infinite repetition of the items in coll"
(setv seen [])
@ -112,7 +116,7 @@
(defn flatten [coll]
"Return a single flat list expanding all members of coll"
(if (and (iterable? coll) (not (string? coll)))
(if (coll? coll)
(_flatten coll [])
(raise (TypeError (.format "{0!r} is not a collection" coll)))))
@ -298,8 +302,8 @@
(_numeric_check n)
(= n 0))
(def *exports* '[calling-module-name cycle dec distinct disassemble drop
drop-while empty? even? filter flatten float? gensym
(def *exports* '[calling-module-name coll? cycle dec distinct disassemble
drop drop-while empty? even? filter flatten float? gensym
inc instance? integer integer? iterable? iterate
iterator? macroexpand macroexpand-1 neg? nil? none?
nth numeric? odd? pos? remove repeat repeatedly second

View File

@ -30,6 +30,14 @@
(defn assert-equal [x y]
(assert (= x y)))
(defn test-coll? []
"NATIVE: testing coll?"
(assert-true (coll? [1 2 3]))
(assert-true (coll? {"a" 1 "b" 2}))
(assert-true (coll? (range 10)))
(assert-false (coll? "abc"))
(assert-false (coll? 1)))
(defn test-cycle []
"NATIVE: testing cycle"
(assert-equal (list (cycle [])) [])