Rewrite language.hy not to require hy.core.macros

As was intended when the bootstrap and core macros were separated.
This commit is contained in:
Nicolas Dandrimont 2013-09-29 18:01:20 +02:00
parent 875d5f2ff5
commit a90c866327

View File

@ -23,8 +23,6 @@
;;;; to make functional programming slightly easier.
;;;;
(require hy.core.macros)
(defn _numeric-check [x]
(if (not (numeric? x))
(raise (TypeError (.format "{0!r} is not a number" x)))))
@ -32,11 +30,11 @@
(defn cycle [coll]
"Yield an infinite repetition of the items in coll"
(setv seen [])
(for [x coll]
(foreach [x coll]
(yield x)
(.append seen x))
(while seen
(for [x seen]
(foreach [x seen]
(yield x))))
(defn dec [n]
@ -48,7 +46,7 @@
"Return a generator from the original collection with duplicates
removed"
(let [[seen []] [citer (iter coll)]]
(for [val citer]
(foreach [val citer]
(if (not_in val seen)
(do
(yield val)
@ -57,7 +55,7 @@
(defn drop [count coll]
"Drop `count` elements from `coll` and yield back the rest"
(let [[citer (iter coll)]]
(try (for [i (range count)]
(try (foreach [i (range count)]
(next citer))
(catch [StopIteration]))
citer))
@ -65,10 +63,10 @@
(defn drop-while [pred coll]
"Drop all elements of `coll` until `pred` is False"
(let [[citer (iter coll)]]
(for [val citer]
(foreach [val citer]
(if (not (pred val))
(do (yield val) (break))))
(for [val citer]
(foreach [val citer]
(yield val))))
(defn empty? [coll]
@ -83,7 +81,7 @@
(defn filter [pred coll]
"Return all elements from `coll` that pass `pred`"
(let [[citer (iter coll)]]
(for [val citer]
(foreach [val citer]
(if (pred val)
(yield val)))))
@ -138,7 +136,7 @@
"Return nth item in collection or sequence, counting from 0"
(if (not (neg? index))
(if (iterable? coll)
(try (first (list (take 1 (drop index coll))))
(try (get (list (take 1 (drop index coll))) 0)
(catch [IndexError] None))
(try (get coll index)
(catch [IndexError] None)))
@ -157,7 +155,7 @@
(defn remove [pred coll]
"Return coll with elements removed that pass `pred`"
(let [[citer (iter coll)]]
(for [val citer]
(foreach [val citer]
(if (not (pred val))
(yield val)))))
@ -165,7 +163,7 @@
"Yield x forever or optionally n times"
(if (none? n)
(setv dispatch (fn [] (while true (yield x))))
(setv dispatch (fn [] (for [_ (range n)] (yield x)))))
(setv dispatch (fn [] (foreach [_ (range n)] (yield x)))))
(dispatch))
(defn repeatedly [func]
@ -187,7 +185,7 @@
"Take `count` elements from `coll`, or the whole set if the total
number of entries in `coll` is less than `count`."
(let [[citer (iter coll)]]
(for [_ (range count)]
(foreach [_ (range count)]
(yield (next citer)))))
(defn take-nth [n coll]
@ -195,16 +193,16 @@
raises ValueError for (not (pos? n))"
(if (pos? n)
(let [[citer (iter coll)] [skip (dec n)]]
(for [val citer]
(foreach [val citer]
(yield val)
(for [_ (range skip)]
(foreach [_ (range skip)]
(next citer))))
(raise (ValueError "n must be positive"))))
(defn take-while [pred coll]
"Take all elements while `pred` is true"
(let [[citer (iter coll)]]
(for [val citer]
(foreach [val citer]
(if (pred val)
(yield val)
(break)))))