Remove uses of let
from /hy
This commit is contained in:
parent
36507b0678
commit
99638ba2df
@ -26,8 +26,8 @@
|
|||||||
(do
|
(do
|
||||||
(import [requests])
|
(import [requests])
|
||||||
|
|
||||||
(let [r (requests.get
|
(setv r (requests.get
|
||||||
"https://raw.githubusercontent.com/hylang/hy/master/AUTHORS")]
|
"https://raw.githubusercontent.com/hylang/hy/master/AUTHORS"))
|
||||||
(repeat r.text)))
|
(repeat r.text))
|
||||||
(except [e ImportError]
|
(except [e ImportError]
|
||||||
(repeat "Botsbuildbots requires `requests' to function."))))
|
(repeat "Botsbuildbots requires `requests' to function."))))
|
||||||
|
@ -58,13 +58,13 @@
|
|||||||
|
|
||||||
|
|
||||||
(defmacro/g! fnr [signature &rest body]
|
(defmacro/g! fnr [signature &rest body]
|
||||||
(let [new-body (recursive-replace 'recur g!recur-fn body)]
|
(setv new-body (recursive-replace 'recur g!recur-fn body))
|
||||||
`(do
|
`(do
|
||||||
(import [hy.contrib.loop [--trampoline--]])
|
(import [hy.contrib.loop [--trampoline--]])
|
||||||
(with-decorator
|
(with-decorator
|
||||||
--trampoline--
|
--trampoline--
|
||||||
(def ~g!recur-fn (fn [~@signature] ~@new-body)))
|
(def ~g!recur-fn (fn [~@signature] ~@new-body)))
|
||||||
~g!recur-fn)))
|
~g!recur-fn))
|
||||||
|
|
||||||
|
|
||||||
(defmacro defnr [name lambda-list &rest body]
|
(defmacro defnr [name lambda-list &rest body]
|
||||||
@ -86,8 +86,8 @@
|
|||||||
;; If recur is used in a non-tail-call position, None is returned, which
|
;; If recur is used in a non-tail-call position, None is returned, which
|
||||||
;; causes chaos. Fixing this to detect if recur is in a tail-call position
|
;; causes chaos. Fixing this to detect if recur is in a tail-call position
|
||||||
;; and erroring if not is a giant TODO.
|
;; and erroring if not is a giant TODO.
|
||||||
(let [fnargs (map (fn [x] (first x)) bindings)
|
(setv fnargs (map (fn [x] (first x)) bindings)
|
||||||
initargs (map second bindings)]
|
initargs (map second bindings))
|
||||||
`(do (require hy.contrib.loop)
|
`(do (require hy.contrib.loop)
|
||||||
(hy.contrib.loop.defnr ~g!recur-fn [~@fnargs] ~@body)
|
(hy.contrib.loop.defnr ~g!recur-fn [~@fnargs] ~@body)
|
||||||
(~g!recur-fn ~@initargs))))
|
(~g!recur-fn ~@initargs)))
|
||||||
|
@ -103,13 +103,12 @@
|
|||||||
(defn distinct [coll]
|
(defn distinct [coll]
|
||||||
"Return a generator from the original collection with duplicates
|
"Return a generator from the original collection with duplicates
|
||||||
removed"
|
removed"
|
||||||
(let [seen (set)
|
(setv seen (set) citer (iter coll))
|
||||||
citer (iter coll)]
|
|
||||||
(for* [val citer]
|
(for* [val citer]
|
||||||
(if (not_in val seen)
|
(if (not_in val seen)
|
||||||
(do
|
(do
|
||||||
(yield val)
|
(yield val)
|
||||||
(.add seen val))))))
|
(.add seen val)))))
|
||||||
|
|
||||||
(if-python2
|
(if-python2
|
||||||
(def
|
(def
|
||||||
@ -178,9 +177,9 @@
|
|||||||
|
|
||||||
(defn drop-last [n coll]
|
(defn drop-last [n coll]
|
||||||
"Return a sequence of all but the last n elements in coll."
|
"Return a sequence of all but the last n elements in coll."
|
||||||
(let [iters (tee coll)]
|
(setv iters (tee coll))
|
||||||
(map first (apply zip [(get iters 0)
|
(map first (apply zip [(get iters 0)
|
||||||
(drop n (get iters 1))]))))
|
(drop n (get iters 1))])))
|
||||||
|
|
||||||
(defn empty? [coll]
|
(defn empty? [coll]
|
||||||
"Return True if `coll` is empty"
|
"Return True if `coll` is empty"
|
||||||
@ -229,14 +228,14 @@
|
|||||||
(setv _gensym_lock (Lock))
|
(setv _gensym_lock (Lock))
|
||||||
|
|
||||||
(defn gensym [&optional [g "G"]]
|
(defn gensym [&optional [g "G"]]
|
||||||
(let [new_symbol None]
|
(setv new_symbol None)
|
||||||
(global _gensym_counter)
|
(global _gensym_counter)
|
||||||
(global _gensym_lock)
|
(global _gensym_lock)
|
||||||
(.acquire _gensym_lock)
|
(.acquire _gensym_lock)
|
||||||
(try (do (setv _gensym_counter (inc _gensym_counter))
|
(try (do (setv _gensym_counter (inc _gensym_counter))
|
||||||
(setv new_symbol (HySymbol (.format ":{0}_{1}" g _gensym_counter))))
|
(setv new_symbol (HySymbol (.format ":{0}_{1}" g _gensym_counter))))
|
||||||
(finally (.release _gensym_lock)))
|
(finally (.release _gensym_lock)))
|
||||||
new_symbol))
|
new_symbol)
|
||||||
|
|
||||||
(defn calling-module-name [&optional [n 1]]
|
(defn calling-module-name [&optional [n 1]]
|
||||||
"Get the name of the module calling `n` levels up the stack from the
|
"Get the name of the module calling `n` levels up the stack from the
|
||||||
@ -335,16 +334,16 @@
|
|||||||
from the latter (left-to-right) will be combined with the mapping in
|
from the latter (left-to-right) will be combined with the mapping in
|
||||||
the result by calling (f val-in-result val-in-latter)."
|
the result by calling (f val-in-result val-in-latter)."
|
||||||
(if (any maps)
|
(if (any maps)
|
||||||
(let [merge-entry (fn [m e]
|
(do
|
||||||
(let [k (get e 0)
|
(defn merge-entry [m e]
|
||||||
v (get e 1)]
|
(setv k (get e 0) v (get e 1))
|
||||||
(if (in k m)
|
(if (in k m)
|
||||||
(assoc m k (f (get m k) v))
|
(assoc m k (f (get m k) v))
|
||||||
(assoc m k v)))
|
(assoc m k v))
|
||||||
m)
|
m)
|
||||||
merge2 (fn [m1 m2]
|
(defn merge2 [m1 m2]
|
||||||
(reduce merge-entry (.items m2) (or m1 {})))]
|
(reduce merge-entry (.items m2) (or m1 {})))
|
||||||
(reduce merge2 maps))))
|
(reduce merge2 maps))))
|
||||||
|
|
||||||
(defn neg? [n]
|
(defn neg? [n]
|
||||||
"Return true if n is < 0"
|
"Return true if n is < 0"
|
||||||
@ -421,13 +420,13 @@
|
|||||||
"Return every nth member of coll
|
"Return every nth member of coll
|
||||||
raises ValueError for (not (pos? n))"
|
raises ValueError for (not (pos? n))"
|
||||||
(if (pos? n)
|
(if (pos? n)
|
||||||
(let [citer (iter coll)
|
(do
|
||||||
skip (dec n)]
|
(setv citer (iter coll) skip (dec n))
|
||||||
(for* [val citer]
|
(for* [val citer]
|
||||||
(yield val)
|
(yield val)
|
||||||
(for* [_ (range skip)]
|
(for* [_ (range skip)]
|
||||||
(next citer))))
|
(next citer))))
|
||||||
(raise (ValueError "n must be positive"))))
|
(raise (ValueError "n must be positive"))))
|
||||||
|
|
||||||
(defn zero? [n]
|
(defn zero? [n]
|
||||||
"Return true if n is 0"
|
"Return true if n is 0"
|
||||||
|
@ -125,8 +125,8 @@
|
|||||||
[(empty? args) `(do ~@body ~@belse)]
|
[(empty? args) `(do ~@body ~@belse)]
|
||||||
[(= (len args) 2) `(for* [~@args] (do ~@body) ~@belse)]
|
[(= (len args) 2) `(for* [~@args] (do ~@body) ~@belse)]
|
||||||
[True
|
[True
|
||||||
(let [alist (cut args 0 None 2)]
|
(setv alist (cut args 0 None 2))
|
||||||
`(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] (do ~@body) ~@belse))]))
|
`(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] (do ~@body) ~@belse)]))
|
||||||
|
|
||||||
|
|
||||||
(defmacro -> [head &rest rest]
|
(defmacro -> [head &rest rest]
|
||||||
@ -151,7 +151,8 @@
|
|||||||
(if (isinstance expression HyExpression)
|
(if (isinstance expression HyExpression)
|
||||||
`(~(first expression) ~f ~@(rest expression))
|
`(~(first expression) ~f ~@(rest expression))
|
||||||
`(~expression ~f)))
|
`(~expression ~f)))
|
||||||
`(let [~f ~form]
|
`(do
|
||||||
|
(setv ~f ~form)
|
||||||
~@(map build-form expressions)
|
~@(map build-form expressions)
|
||||||
~f))
|
~f))
|
||||||
|
|
||||||
@ -203,23 +204,24 @@
|
|||||||
(defmacro with-gensyms [args &rest body]
|
(defmacro with-gensyms [args &rest body]
|
||||||
(setv syms [])
|
(setv syms [])
|
||||||
(for* [arg args]
|
(for* [arg args]
|
||||||
(.extend syms `[~arg (gensym '~arg)]))
|
(.extend syms [arg `(gensym '~arg)]))
|
||||||
`(let ~syms
|
`(do
|
||||||
~@body))
|
(setv ~@syms)
|
||||||
|
~@body))
|
||||||
|
|
||||||
(defmacro defmacro/g! [name args &rest body]
|
(defmacro defmacro/g! [name args &rest body]
|
||||||
(let [syms (list
|
(setv syms (list
|
||||||
(distinct
|
(distinct
|
||||||
(filter (fn [x]
|
(filter (fn [x]
|
||||||
(and (hasattr x "startswith")
|
(and (hasattr x "startswith")
|
||||||
(.startswith x "g!")))
|
(.startswith x "g!")))
|
||||||
(flatten body))))
|
(flatten body))))
|
||||||
gensyms []]
|
gensyms [])
|
||||||
(for* [sym syms]
|
(for* [sym syms]
|
||||||
(.extend gensyms `[~sym (gensym (cut '~sym 2))]))
|
(.extend gensyms [sym `(gensym ~(cut sym 2))]))
|
||||||
`(defmacro ~name [~@args]
|
`(defmacro ~name [~@args]
|
||||||
(let ~gensyms
|
(setv ~@gensyms)
|
||||||
~@body))))
|
~@body))
|
||||||
|
|
||||||
(defmacro defmacro! [name args &rest body]
|
(defmacro defmacro! [name args &rest body]
|
||||||
"Like defmacro/g! plus automatic once-only evaluation for o!
|
"Like defmacro/g! plus automatic once-only evaluation for o!
|
||||||
@ -251,17 +253,15 @@
|
|||||||
|
|
||||||
(defmacro defmain [args &rest body]
|
(defmacro defmain [args &rest body]
|
||||||
"Write a function named \"main\" and do the if __main__ dance"
|
"Write a function named \"main\" and do the if __main__ dance"
|
||||||
(let [retval (gensym)
|
(setv retval (gensym))
|
||||||
mainfn `(fn [~@args]
|
`(when (= --name-- "__main__")
|
||||||
~@body)]
|
(import sys)
|
||||||
`(when (= --name-- "__main__")
|
(setv ~retval (apply (fn [~@args] ~@body) sys.argv))
|
||||||
(import sys)
|
(if (integer? ~retval)
|
||||||
(setv ~retval (apply ~mainfn sys.argv))
|
(sys.exit ~retval))))
|
||||||
(if (integer? ~retval)
|
|
||||||
(sys.exit ~retval)))))
|
|
||||||
|
|
||||||
|
|
||||||
(defreader @ [expr]
|
(defreader @ [expr]
|
||||||
(let [decorators (cut expr None -1)
|
(setv decorators (cut expr None -1)
|
||||||
fndef (get expr -1)]
|
fndef (get expr -1))
|
||||||
`(with-decorator ~@decorators ~fndef)))
|
`(with-decorator ~@decorators ~fndef))
|
||||||
|
@ -26,22 +26,22 @@
|
|||||||
|
|
||||||
(defn + [&rest args]
|
(defn + [&rest args]
|
||||||
"Shadow + operator for when we need to import / map it against something"
|
"Shadow + operator for when we need to import / map it against something"
|
||||||
(let [count (len args)]
|
(if
|
||||||
(if (zero? count)
|
(= (len args) 1)
|
||||||
(raise (TypeError "Need at least 1 argument to add/concatenate"))
|
(operator.pos (get args 0))
|
||||||
(if (= count 1)
|
args
|
||||||
(operator.pos (get args 0))
|
(reduce operator.add args)
|
||||||
(reduce operator.add args)))))
|
(raise (TypeError "Need at least 1 argument to add/concatenate"))))
|
||||||
|
|
||||||
|
|
||||||
(defn - [&rest args]
|
(defn - [&rest args]
|
||||||
"Shadow - operator for when we need to import / map it against something"
|
"Shadow - operator for when we need to import / map it against something"
|
||||||
(let [count (len args)]
|
(if
|
||||||
(if (= count 0)
|
(= (len args) 1)
|
||||||
(raise (TypeError "Need at least 1 argument to subtract"))
|
(- (get args 0))
|
||||||
(if (= count 1)
|
args
|
||||||
(- (get args 0))
|
(reduce operator.sub args)
|
||||||
(reduce operator.sub args)))))
|
(raise (TypeError "Need at least 1 argument to subtract"))))
|
||||||
|
|
||||||
|
|
||||||
(defn * [&rest args]
|
(defn * [&rest args]
|
||||||
@ -53,12 +53,12 @@
|
|||||||
|
|
||||||
(defn / [&rest args]
|
(defn / [&rest args]
|
||||||
"Shadow / operator for when we need to import / map it against something"
|
"Shadow / operator for when we need to import / map it against something"
|
||||||
(let [count (len args)]
|
(if
|
||||||
(if (= count 0)
|
(= (len args) 1)
|
||||||
(raise (TypeError "Need at least 1 argument to divide"))
|
(operator.truediv 1 (get args 0))
|
||||||
(if (= count 1)
|
args
|
||||||
(operator.truediv 1 (get args 0))
|
(reduce operator.truediv args)
|
||||||
(reduce operator.truediv args)))))
|
(raise (TypeError "Need at least 1 argument to divide"))))
|
||||||
|
|
||||||
|
|
||||||
(defn comp-op [op args]
|
(defn comp-op [op args]
|
||||||
|
@ -26,8 +26,9 @@
|
|||||||
|
|
||||||
|
|
||||||
(defmacro ap-if [test-form then-form &optional else-form]
|
(defmacro ap-if [test-form then-form &optional else-form]
|
||||||
`(let [it ~test-form]
|
`(do
|
||||||
(if it ~then-form ~else-form)))
|
(setv it ~test-form)
|
||||||
|
(if it ~then-form ~else-form)))
|
||||||
|
|
||||||
|
|
||||||
(defmacro ap-each [lst &rest body]
|
(defmacro ap-each [lst &rest body]
|
||||||
@ -38,38 +39,44 @@
|
|||||||
(defmacro ap-each-while [lst form &rest body]
|
(defmacro ap-each-while [lst form &rest body]
|
||||||
"Evaluate the body form for each element in the list while the
|
"Evaluate the body form for each element in the list while the
|
||||||
predicate form evaluates to True."
|
predicate form evaluates to True."
|
||||||
`(let [p (lambda [it] ~form)]
|
(setv p (gensym))
|
||||||
|
`(do
|
||||||
|
(defn ~p [it] ~form)
|
||||||
(for [it ~lst]
|
(for [it ~lst]
|
||||||
(if (p it)
|
(if (~p it)
|
||||||
~@body
|
~@body
|
||||||
(break)))))
|
(break)))))
|
||||||
|
|
||||||
|
|
||||||
(defmacro ap-map [form lst]
|
(defmacro ap-map [form lst]
|
||||||
"Yield elements evaluated in the form for each element in the list."
|
"Yield elements evaluated in the form for each element in the list."
|
||||||
(let [v (gensym 'v)
|
(setv v (gensym 'v) f (gensym 'f))
|
||||||
f (gensym 'f)]
|
`((fn []
|
||||||
`(let [~f (lambda [it] ~form)]
|
(defn ~f [it] ~form)
|
||||||
(for [~v ~lst]
|
(for [~v ~lst]
|
||||||
(yield (~f ~v))))))
|
(yield (~f ~v))))))
|
||||||
|
|
||||||
|
|
||||||
(defmacro ap-map-when [predfn rep lst]
|
(defmacro ap-map-when [predfn rep lst]
|
||||||
"Yield elements evaluated for each element in the list when the
|
"Yield elements evaluated for each element in the list when the
|
||||||
predicate function returns True."
|
predicate function returns True."
|
||||||
`(let [f (lambda [it] ~rep)]
|
(setv f (gensym))
|
||||||
|
`((fn []
|
||||||
|
(defn ~f [it] ~rep)
|
||||||
(for [it ~lst]
|
(for [it ~lst]
|
||||||
(if (~predfn it)
|
(if (~predfn it)
|
||||||
(yield (f it))
|
(yield (~f it))
|
||||||
(yield it)))))
|
(yield it))))))
|
||||||
|
|
||||||
|
|
||||||
(defmacro ap-filter [form lst]
|
(defmacro ap-filter [form lst]
|
||||||
"Yield elements returned when the predicate form evaluates to True."
|
"Yield elements returned when the predicate form evaluates to True."
|
||||||
`(let [pred (lambda [it] ~form)]
|
(setv pred (gensym))
|
||||||
|
`((fn []
|
||||||
|
(defn ~pred [it] ~form)
|
||||||
(for [val ~lst]
|
(for [val ~lst]
|
||||||
(if (pred val)
|
(if (~pred val)
|
||||||
(yield val)))))
|
(yield val))))))
|
||||||
|
|
||||||
|
|
||||||
(defmacro ap-reject [form lst]
|
(defmacro ap-reject [form lst]
|
||||||
@ -80,14 +87,15 @@
|
|||||||
(defmacro ap-dotimes [n &rest body]
|
(defmacro ap-dotimes [n &rest body]
|
||||||
"Execute body for side effects `n' times, with it bound from 0 to n-1"
|
"Execute body for side effects `n' times, with it bound from 0 to n-1"
|
||||||
(unless (numeric? n)
|
(unless (numeric? n)
|
||||||
(raise (TypeError (.format "{0!r} is not a number" n))))
|
(raise (TypeError (.format "{!r} is not a number" n))))
|
||||||
`(ap-each (range ~n) ~@body))
|
`(ap-each (range ~n) ~@body))
|
||||||
|
|
||||||
|
|
||||||
(defmacro ap-first [predfn lst]
|
(defmacro ap-first [predfn lst]
|
||||||
"Yield the first element that passes `predfn`"
|
"Yield the first element that passes `predfn`"
|
||||||
(with-gensyms [n]
|
(with-gensyms [n]
|
||||||
`(let [~n None]
|
`(do
|
||||||
|
(setv ~n None)
|
||||||
(ap-each ~lst (when ~predfn (setv ~n it) (break)))
|
(ap-each ~lst (when ~predfn (setv ~n it) (break)))
|
||||||
~n)))
|
~n)))
|
||||||
|
|
||||||
@ -95,7 +103,8 @@
|
|||||||
(defmacro ap-last [predfn lst]
|
(defmacro ap-last [predfn lst]
|
||||||
"Yield the last element that passes `predfn`"
|
"Yield the last element that passes `predfn`"
|
||||||
(with-gensyms [n]
|
(with-gensyms [n]
|
||||||
`(let [~n None]
|
`(do
|
||||||
|
(setv ~n None)
|
||||||
(ap-each ~lst (none? ~n)
|
(ap-each ~lst (none? ~n)
|
||||||
(when ~predfn
|
(when ~predfn
|
||||||
(setv ~n it)))
|
(setv ~n it)))
|
||||||
@ -104,20 +113,18 @@
|
|||||||
|
|
||||||
(defmacro ap-reduce [form lst &optional [initial-value None]]
|
(defmacro ap-reduce [form lst &optional [initial-value None]]
|
||||||
"Anaphoric form of reduce, `acc' and `it' can be used for a form"
|
"Anaphoric form of reduce, `acc' and `it' can be used for a form"
|
||||||
(if (none? initial-value)
|
`(do
|
||||||
`(let [acc (car ~lst)]
|
(setv acc ~(if (none? initial-value) `(car ~lst) initial-value))
|
||||||
(ap-each (cdr ~lst) (setv acc ~form))
|
(ap-each ~(if (none? initial-value) `(cdr ~lst) lst)
|
||||||
acc)
|
(setv acc ~form))
|
||||||
`(let [acc ~initial-value]
|
acc))
|
||||||
(ap-each ~lst (setv acc ~form))
|
|
||||||
acc)))
|
|
||||||
|
|
||||||
|
|
||||||
(defmacro ap-pipe [var &rest forms]
|
(defmacro ap-pipe [var &rest forms]
|
||||||
"Pushes a value through several forms.
|
"Pushes a value through several forms.
|
||||||
(Anaphoric version of -> and ->>)"
|
(Anaphoric version of -> and ->>)"
|
||||||
(if (empty? forms) var
|
(if (empty? forms) var
|
||||||
`(ap-pipe (let [it ~var] ~(first forms)) ~@(rest forms))))
|
`(ap-pipe (do (setv it ~var) ~(first forms)) ~@(rest forms))))
|
||||||
|
|
||||||
|
|
||||||
(defmacro ap-compose [&rest forms]
|
(defmacro ap-compose [&rest forms]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user