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