From e575898b7a940568a051f8e2fbfa92c967e5687b Mon Sep 17 00:00:00 2001 From: Adam Jeniski Date: Thu, 9 Jan 2025 14:17:41 -0500 Subject: [PATCH] add n-map --- shared/clj/src/core.clj | 52 +++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/shared/clj/src/core.clj b/shared/clj/src/core.clj index 2d29cac..30b8436 100644 --- a/shared/clj/src/core.clj +++ b/shared/clj/src/core.clj @@ -23,7 +23,6 @@ (recur m (assoc res (.start m) (.group m))) res))) - ;; general utils (defn dbg @@ -33,10 +32,14 @@ (->> x some-fn dbg + (dbg \"optional tag/prefix:\") some-fn-2)" - [x] - (println x) - x) + ([x] + (println x) + x) + ([prefix x] + (println prefix x) + x)) (defn log "faster than dbg, for those really tricky graph problems @@ -58,7 +61,6 @@ ([f1 f2 f3 f4 & fs] (comp (apply comp (reverse fs)) f4 f3 f2 f1))) - ;; alter collections (defn get-coords [list-of-lists] @@ -72,11 +74,38 @@ (map (juxt identity #(get (get arr-2d (first %)) (second %)))) (into {}))) +(defn map-to-coords [arr-2d] + (->> arr-2d + get-coords + (map (juxt #(get (get arr-2d (first %)) (second %)) identity)) + (into {}))) + (defn insert-at-idx [coll idx el] (concat (take idx coll) (list el) (drop idx coll))) +(defn n-map + "(map map map... f coll) with n maps times" + [n f & colls] + (loop [n n + mapping-f f] + (cond + (<= n 0) (apply mapping-f colls) + (= 1 n) (apply map mapping-f colls) + :else (recur (dec n) + (partial map mapping-f))))) + +(defn n-mapv + "(mapv mapv mapv... f coll) with n maps times" + [n f & colls] + (loop [n n + mapping-f f] + (if (= 1 n) + (apply mapv mapping-f colls) + (recur (dec n) + (partial mapv mapping-f))))) + (defn mmap "map map f coll" [f & colls] @@ -87,16 +116,6 @@ [f & colls] (apply mapv (partial mapv f) colls)) -(defn mmmap - "map map map f coll" - [f & colls] - (apply map (partial map (partial map f)) colls)) - -(defn mmmapv - "mapv mapv mapv f coll" - [f & colls] - (apply mapv (partial mapv (partial mapv f)) colls)) - (defn partition-by-counts [counts coll] (->> counts (reduce (fn [[acc coll] c] @@ -122,7 +141,6 @@ :last (first sorted-nums)} (rest sorted-nums)))) - ;; Math things (defn square [n] (* n n)) @@ -139,7 +157,6 @@ el2 b] (* el1 el2)))) - ;; conversions (defn binary->long [binary-str] @@ -158,7 +175,6 @@ [condition] (if condition 1 0)) - ;; 👻 macros 👻 (defmacro pfor