fix whitespace in anaphoric

This commit is contained in:
gilch 2017-10-26 12:53:08 -06:00
parent a5146e6494
commit 2319adcc7f
2 changed files with 40 additions and 41 deletions

View File

@ -8,8 +8,8 @@
(defmacro ap-if [test-form then-form &optional else-form]
`(do
(setv it ~test-form)
(if it ~then-form ~else-form)))
(setv it ~test-form)
(if it ~then-form ~else-form)))
(defmacro ap-each [lst &rest body]
@ -25,17 +25,17 @@
(defn ~p [it] ~form)
(for [it ~lst]
(if (~p it)
~@body
(break)))))
~@body
(break)))))
(defmacro ap-map [form lst]
"Yield elements evaluated in the form for each element in the list."
(setv v (gensym 'v) f (gensym 'f))
`((fn []
(defn ~f [it] ~form)
(for [~v ~lst]
(yield (~f ~v))))))
(defn ~f [it] ~form)
(for [~v ~lst]
(yield (~f ~v))))))
(defmacro ap-map-when [predfn rep lst]
@ -43,21 +43,21 @@
predicate function returns True."
(setv f (gensym))
`((fn []
(defn ~f [it] ~rep)
(for [it ~lst]
(if (~predfn it)
(yield (~f it))
(yield it))))))
(defn ~f [it] ~rep)
(for [it ~lst]
(if (~predfn it)
(yield (~f it))
(yield it))))))
(defmacro ap-filter [form lst]
"Yield elements returned when the predicate form evaluates to True."
(setv pred (gensym))
`((fn []
(defn ~pred [it] ~form)
(for [val ~lst]
(if (~pred val)
(yield val))))))
(defn ~pred [it] ~form)
(for [val ~lst]
(if (~pred val)
(yield val))))))
(defmacro ap-reject [form lst]
@ -95,10 +95,10 @@
(defmacro ap-reduce [form lst &optional [initial-value None]]
"Anaphoric form of reduce, `acc' and `it' can be used for a form"
`(do
(setv acc ~(if (none? initial-value) `(get ~lst 0) initial-value))
(ap-each ~(if (none? initial-value) `(cut ~lst 1) lst)
(setv acc ~form))
acc))
(setv acc ~(if (none? initial-value) `(get ~lst 0) initial-value))
(ap-each ~(if (none? initial-value) `(cut ~lst 1) lst)
(setv acc ~form))
acc))
(defmacro ap-pipe [var &rest forms]
@ -119,19 +119,18 @@
This is not a replacement for fn. The xi forms cannot be nested. "
(setv flatbody (flatten body))
`(fn [;; generate all xi symbols up to the maximum found in body
~@(genexpr (HySymbol (+ "x"
(str i)))
[i (range 1
;; find the maximum xi
(inc (max (+ (list-comp (int (cut a 1))
[a flatbody]
(and (symbol? a)
(.startswith a 'x)
(.isdigit (cut a 1))))
[0]))))])
;; generate the &rest parameter only if 'xi is present in body
~@(if (in 'xi flatbody)
'(&rest xi)
'())]
~@(genexpr (HySymbol (+ "x"
(str i)))
[i (range 1
;; find the maximum xi
(inc (max (+ (list-comp (int (cut a 1))
[a flatbody]
(and (symbol? a)
(.startswith a 'x)
(.isdigit (cut a 1))))
[0]))))])
;; generate the &rest parameter only if 'xi is present in body
~@(if (in 'xi flatbody)
'(&rest xi)
'())]
(~@body)))

View File

@ -65,9 +65,9 @@
(defn test-ap-dotimes []
"NATIVE: testing anaphoric dotimes"
(assert-equal (do (setv n []) (ap-dotimes 3 (.append n 3)) n)
[3 3 3])
[3 3 3])
(assert-equal (do (setv n []) (ap-dotimes 3 (.append n it)) n)
[0 1 2]))
[0 1 2]))
(defn test-ap-first []
"NATIVE: testing anaphoric first"
@ -86,7 +86,7 @@
(assert-equal (ap-reduce (* acc it) [1 2 3]) 6)
(assert-equal (ap-reduce (* acc it) [1 2 3] 6) 36)
(assert-equal (ap-reduce (+ acc " on " it) ["Hy" "meth"])
"Hy on meth")
"Hy on meth")
(assert-equal (ap-reduce (+ acc it) [] 1) 1))
(defn test-ap-pipe []