Rationalize anaphoric parameter names

This commit is contained in:
Kodi Arfer 2019-12-21 13:33:47 -05:00
parent e57bbb92db
commit bbb7426269

View File

@ -11,39 +11,39 @@
(if it ~then-form ~else-form)))) (if it ~then-form ~else-form))))
(defmacro ap-each [lst &rest body] (defmacro ap-each [xs &rest body]
"Evaluate the body form for each element in the list." "Evaluate the body form for each element in the list."
(rit `(for [it ~lst] ~@body))) (rit `(for [it ~xs] ~@body)))
(defmacro ap-each-while [lst form &rest body] (defmacro ap-each-while [xs 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."
(rit `(for [it ~lst] (rit `(for [it ~xs]
(unless ~form (unless ~form
(break)) (break))
~@body))) ~@body)))
(defmacro ap-map [form lst] (defmacro ap-map [form xs]
"Yield elements evaluated in the form for each element in the list." "Yield elements evaluated in the form for each element in the list."
(rit `(gfor it ~lst ~form))) (rit `(gfor it ~xs ~form)))
(defmacro ap-map-when [predfn rep lst] (defmacro ap-map-when [predfn rep xs]
"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."
(rit `(gfor it ~lst (if (~predfn it) ~rep it)))) (rit `(gfor it ~xs (if (~predfn it) ~rep it))))
(defmacro ap-filter [form lst] (defmacro ap-filter [form xs]
"Yield elements returned when the predicate form evaluates to True." "Yield elements returned when the predicate form evaluates to True."
(rit `(gfor it ~lst :if ~form it))) (rit `(gfor it ~xs :if ~form it)))
(defmacro ap-reject [form lst] (defmacro ap-reject [form xs]
"Yield elements returned when the predicate form evaluates to False" "Yield elements returned when the predicate form evaluates to False"
(rit `(gfor it ~lst :if (not ~form) it))) (rit `(gfor it ~xs :if (not ~form) it)))
(defmacro ap-dotimes [n &rest body] (defmacro ap-dotimes [n &rest body]
@ -52,32 +52,32 @@
~@body))) ~@body)))
(defmacro ap-first [predfn lst] (defmacro ap-first [form xs]
"Yield the first element that passes `predfn`" "Yield the first element that passes `form`"
(rit `(next (rit `(next
(gfor it ~lst :if ~predfn it) (gfor it ~xs :if ~form it)
None))) None)))
(defmacro ap-last [predfn lst] (defmacro ap-last [form xs]
"Yield the last element that passes `predfn`" "Yield the last element that passes `form`"
(setv x (gensym)) (setv x (gensym))
(rit `(do (rit `(do
(setv ~x None) (setv ~x None)
(for [it ~lst :if ~predfn] (for [it ~xs :if ~form]
(setv ~x it)) (setv ~x it))
~x))) ~x)))
(defmacro! ap-reduce [form o!lst &optional [initial-value None]] (defmacro! ap-reduce [form o!xs &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"
(recur-sym-replace {'it (gensym) 'acc (gensym)} `(do (recur-sym-replace {'it (gensym) 'acc (gensym)} `(do
(setv acc ~(if (none? initial-value) (setv acc ~(if (none? initial-value)
`(do `(do
(setv ~g!lst (iter ~g!lst)) (setv ~g!xs (iter ~g!xs))
(next ~g!lst)) (next ~g!xs))
initial-value)) initial-value))
(for [it ~g!lst] (for [it ~g!xs]
(setv acc ~form)) (setv acc ~form))
acc))) acc)))