mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 13:03:19 -09:00
improve macros
This commit is contained in:
parent
8300fce484
commit
0296821b56
@ -28,10 +28,44 @@
|
|||||||
(defn split-spaces [s]
|
(defn split-spaces [s]
|
||||||
(string/split s #" "))
|
(string/split s #" "))
|
||||||
|
|
||||||
|
(defmacro let-dbg [arg-list & body]
|
||||||
|
(let [pairs (partition 2 arg-list)
|
||||||
|
definitions (->> pairs
|
||||||
|
(map (fn [[a b]]
|
||||||
|
`[~a (let [temp# ~b]
|
||||||
|
(println '~a "=" temp#)
|
||||||
|
temp#)]))
|
||||||
|
(apply concat))]
|
||||||
|
`(let [~@definitions]
|
||||||
|
~@body)))
|
||||||
|
|
||||||
(defmacro comp>
|
(defmacro comp>
|
||||||
"comp, but fn-args are applied from left to right"
|
"comp, but fn-args are composed from left to right with currying
|
||||||
[& ls] (let [r# (reverse ls)]
|
previous result inserted as first element to next function application"
|
||||||
`(comp ~@r#)))
|
[& ls]
|
||||||
|
(let [fs (map (fn [l]
|
||||||
|
(if (and (list? l)
|
||||||
|
(not= 'fn (first l)))
|
||||||
|
(let [[f & fargs] l]
|
||||||
|
`(fn [v#] (~f v# ~@fargs)))
|
||||||
|
l))
|
||||||
|
ls)
|
||||||
|
r (reverse fs)]
|
||||||
|
`(comp ~@r)))
|
||||||
|
|
||||||
|
(defmacro comp>>
|
||||||
|
"comp, but fn-args are composed from left to right with currying
|
||||||
|
previous result inserted as last element to next function application"
|
||||||
|
[& ls]
|
||||||
|
(let [fs (map (fn [l]
|
||||||
|
(if (and (list? l)
|
||||||
|
(not= 'fn (first l)))
|
||||||
|
(let [[f & fargs] l]
|
||||||
|
`(fn [v#] (~f ~@fargs v#)))
|
||||||
|
l))
|
||||||
|
ls)
|
||||||
|
r (reverse fs)]
|
||||||
|
`(comp ~@r)))
|
||||||
|
|
||||||
(defmacro w-fn
|
(defmacro w-fn
|
||||||
"wraps s-expr such as java static methods or macro calls
|
"wraps s-expr such as java static methods or macro calls
|
||||||
@ -97,27 +131,55 @@
|
|||||||
(last args))
|
(last args))
|
||||||
(mapv #((first %) (second %)))))
|
(mapv #((first %) (second %)))))
|
||||||
|
|
||||||
|
(def p partial)
|
||||||
|
|
||||||
|
(defmacro assert= [& abs]
|
||||||
|
`(do ~@(map (fn [[a b]]
|
||||||
|
`(assert (= ~a ~b)))
|
||||||
|
(partition 2 abs))))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
|
|
||||||
|
(let-dbg [a 1
|
||||||
|
b (inc a)]
|
||||||
|
(+ a b))
|
||||||
|
|
||||||
|
(defn abc [a b]
|
||||||
|
(+ a b))
|
||||||
|
|
||||||
|
(assert (= 1
|
||||||
|
(inc 0)
|
||||||
|
(dec 2)
|
||||||
|
(+ (/ 1 2)
|
||||||
|
(/ 1 2))))
|
||||||
|
|
||||||
|
(assert=
|
||||||
|
8 ((comp> (+ 1) (/ 2) (abc 1) (inc) ;; currying / partial fn applicaiton
|
||||||
|
inc ;; named fn
|
||||||
|
#(+ 1 %) (fn [v] (+ v 1))) ;; using lambdas
|
||||||
|
5)
|
||||||
|
|
||||||
|
3 ((comp> (- 5) (- 2))
|
||||||
|
10)
|
||||||
|
|
||||||
|
7 ((comp>> (- 5) (- 2))
|
||||||
|
10))
|
||||||
|
|
||||||
(map (w-fn Long/parseLong) ["123" "456"])
|
(map (w-fn Long/parseLong) ["123" "456"])
|
||||||
input-cache
|
input-cache
|
||||||
|
|
||||||
((mapvf #(* 2 %)) [1 2])
|
((mapvf #(* 2 %)) [1 2])
|
||||||
((reducef + 0) [1 2])
|
((reducef + 0) [1 2])
|
||||||
|
|
||||||
((comp> #(* % 3)
|
(let [v 20]
|
||||||
#(+ % 2)
|
(assert= ((comp> #(* % 3) #(+ % 2) #(* % 4))
|
||||||
#(* % 4))
|
v)
|
||||||
5)
|
((comp #(* % 4) #(+ % 2) #(* % 3))
|
||||||
|
v)))
|
||||||
|
|
||||||
;; is always 1, comp> is comp but reversed (to be the proper way round lol)
|
(assert=
|
||||||
(->> 20
|
(apply-each #(* % 2) #(+ % 5) 5 10)
|
||||||
((juxt (comp> #(* % 3) #(+ % 2) #(* % 4))
|
(apply-each-v #(* % 2) #(+ % 5) [5 10]))
|
||||||
(comp #(* % 4) #(+ % 2) #(* % 3))))
|
|
||||||
distinct
|
|
||||||
count)
|
|
||||||
|
|
||||||
(apply-each #(* % 2) #(+ % 5) 5 10)
|
|
||||||
(apply-each-v #(* % 2) #(+ % 5) [5 10])
|
|
||||||
|
|
||||||
(log (+ 5 5)
|
(log (+ 5 5)
|
||||||
(- 2 1))
|
(- 2 1))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user