add n-map

This commit is contained in:
Adam Jeniski 2025-01-09 14:17:41 -05:00
parent f15dc986db
commit e575898b7a

View File

@ -23,7 +23,6 @@
(recur m (assoc res (.start m) (.group m))) (recur m (assoc res (.start m) (.group m)))
res))) res)))
;; general utils ;; general utils
(defn dbg (defn dbg
@ -33,10 +32,14 @@
(->> x (->> x
some-fn some-fn
dbg dbg
(dbg \"optional tag/prefix:\")
some-fn-2)" some-fn-2)"
[x] ([x]
(println x) (println x)
x) x)
([prefix x]
(println prefix x)
x))
(defn log (defn log
"faster than dbg, for those really tricky graph problems "faster than dbg, for those really tricky graph problems
@ -58,7 +61,6 @@
([f1 f2 f3 f4 & fs] ([f1 f2 f3 f4 & fs]
(comp (apply comp (reverse fs)) f4 f3 f2 f1))) (comp (apply comp (reverse fs)) f4 f3 f2 f1)))
;; alter collections ;; alter collections
(defn get-coords [list-of-lists] (defn get-coords [list-of-lists]
@ -72,11 +74,38 @@
(map (juxt identity #(get (get arr-2d (first %)) (second %)))) (map (juxt identity #(get (get arr-2d (first %)) (second %))))
(into {}))) (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] (defn insert-at-idx [coll idx el]
(concat (take idx coll) (concat (take idx coll)
(list el) (list el)
(drop idx coll))) (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 (defn mmap
"map map f coll" "map map f coll"
[f & colls] [f & colls]
@ -87,16 +116,6 @@
[f & colls] [f & colls]
(apply mapv (partial mapv 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] (defn partition-by-counts [counts coll]
(->> counts (->> counts
(reduce (fn [[acc coll] c] (reduce (fn [[acc coll] c]
@ -122,7 +141,6 @@
:last (first sorted-nums)} :last (first sorted-nums)}
(rest sorted-nums)))) (rest sorted-nums))))
;; Math things ;; Math things
(defn square [n] (* n n)) (defn square [n] (* n n))
@ -139,7 +157,6 @@
el2 b] el2 b]
(* el1 el2)))) (* el1 el2))))
;; conversions ;; conversions
(defn binary->long [binary-str] (defn binary->long [binary-str]
@ -158,7 +175,6 @@
[condition] [condition]
(if condition 1 0)) (if condition 1 0))
;; 👻 macros 👻 ;; 👻 macros 👻
(defmacro pfor (defmacro pfor