Skip to content

Instantly share code, notes, and snippets.

@evilsneer
Last active February 20, 2021 13:01
Show Gist options
  • Save evilsneer/a47e90ef3d133589cb26474fcecd34ba to your computer and use it in GitHub Desktop.
Save evilsneer/a47e90ef3d133589cb26474fcecd34ba to your computer and use it in GitHub Desktop.
(defn csv-data->maps [csv-data]
(map zipmap
(->> (first csv-data) ;; First row is the header
(map keyword) ;; Drop if you want string keys instead
repeat)
(rest csv-data)))
(defn transpose [m]
(apply mapv vector m))
(defn map-vals [f m]
(zipmap (keys m) (map f (vals m))))
(defn map-keys [f m]
(zipmap (map f (keys m)) (vals m)))
;; @todo to prewalk
(defn hydrate [xs [from-key to-key] getter]
(let [xs-keys-uniq (->> xs
(map from-key)
(into #{}))
mapper (zipmap xs-keys-uniq (pmap getter xs-keys-uniq))]
(->> xs
(map #(assoc %
to-key
(mapper (from-key %)))))))
(defn map-k-v-on [coll to-level [kf vf]
{:keys [into-coll _start-level]
:or {into-coll []
_start-level 1}
:as opts}]
(if (= _start-level to-level)
(->> coll
(map (fn [[k v]]
[(kf k) (vf v)]))
(into into-coll))
(->> coll
(map (fn [[k v]]
[k (map-k-v-on v to-level [kf vf]
(assoc opts
:start-level (inc _start-level)))])))))
(defn walk-collect2 [start-item &
{:keys [need-to-unfold? unfold-as exclude]
:or {need-to-unfold? coll?
unfold-as identity
exclude (constantly false)}}]
(->> start-item
(tree-seq need-to-unfold? unfold-as)
(filter (comp not exclude))))
;; @todo finish
(defn sort-cols-fn [rows]
(let [first-cols (reverse [:id :name :description])]
(->>
rows
(into (sorted-map-by (fn [a b]
(>=
(.indexOf first-cols a)
(.indexOf first-cols b))))))))
;; @todo add salt
(defn hash-it [x]
(-> (str x salt)
hash/sha256
cod/bytes->hex))
(defn uuid []
(.toString (UUID/randomUUID)))
;;
(defn ->log-> [x & {:keys [text convert-fn] :or {convert-fn identity}}]
(log/info text (convert-fn x))
x)
(defn ->atom-seq-> [value a]
(swap! a conj value)
value)
;;
(defn increasing-repeat [xs]
"[1 2 3] -> [(1) (1 2) (1 2 3)]"
(->>
xs
(repeat (count xs))
(map-indexed vector)
(map (fn [[i v]]
(take (inc i) v)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment