Skip to content

Instantly share code, notes, and snippets.

@rplevy
Last active May 29, 2019 23:15
Show Gist options
  • Save rplevy/e794f00e44f68275e673730622442643 to your computer and use it in GitHub Desktop.
Save rplevy/e794f00e44f68275e673730622442643 to your computer and use it in GitHub Desktop.
takes a sequence of rows of clojure data and a sequence of fns (keys for example) that produce the columns, and generates an org table
(defn clj->org
[data-rows col-fns]
(doseq [line (map (apply juxt col-fns) data-rows)]
(println (str "|" (clojure.string/join "|" line) "|"))))
;; edit: use print-table, and support col headers
;; I never realized print-table is an org-table!!
(defn clj->org
[data-rows col-fns & [headers]]
(clojure.pprint/print-table
(let [headers' (or headers (map (fn [f] (if (keyword? f) f (name (gensym))))
col-fns))]
(map
(comp
#(apply hash-map (interleave headers' %))
(apply juxt col-fns))
data-rows))))
;; edit: actually, with print-table we'd be better off just updating
;; the maps and before feeding them to print-table and then you'd have
;; sensible column names for the calculated columns too
(->> rows (map #(assoc % :foo (my-fn (:bar %) (:baz %)))) print-table)
;; good to know that print-table is an org table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment