From fcdf3c6c9de639889fce224464d84aac9b626cae Mon Sep 17 00:00:00 2001 From: Adam Jeniski Date: Mon, 18 Dec 2023 21:58:07 -0500 Subject: [PATCH] add apply-each and aliases --- 2023/src/core.clj | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/2023/src/core.clj b/2023/src/core.clj index f533f9b..f789774 100644 --- a/2023/src/core.clj +++ b/2023/src/core.clj @@ -71,10 +71,42 @@ `(do ~@exprs# nil))) +(defn apply-each + "[& fs vs] + fs is a list of funcitons of length N + vs is a list of values of length N + + this function returns a vector where each value is the result of applying + an f from fs to the corresponding v from vs" + [& args] + (let [n (/ (count args) 2)] + (->> (zipmap (take n args) + (take-last n args)) + (mapv #((first %) (second %)))))) + +(defn apply-each-v + "[& fs vs] + fs is a list of funcitons of length N + v is a vector of values of length N + + this function returns a vector where each value is the result of applying + an f from fs to the corresponding v from vs" + [& args] + (->> (zipmap (drop-last args) + (last args)) + (mapv #((first %) (second %))))) + +(def p partial) + +(def a apply) + (comment (map (static-fn Long/parseLong) ["123" "456"]) input-cache + (apply-each #(* % 2) #(+ % 5) 5 10) + (apply-each-v #(* % 2) #(+ % 5) [5 10]) + (log (+ 5 5) (- 2 1))