Adding a simple coll? function to the core

* hy/core/language.hy:
  -Added a simple coll? function that checks whether the given argument
  is an iterable and not a string,
  - Also replaced the check in `flatten` by coll?

* tests/native_tests/core.hy: Tests updated for checking coll?
This commit is contained in:
Abhishek L 2014-01-12 15:49:09 +05:30
parent 48be005fa3
commit f159f1499b
2 changed files with 15 additions and 3 deletions

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 [])) [])