Skip to content

Instantly share code, notes, and snippets.

@lewang
Created April 11, 2017 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewang/30b7633586d2fe4a56673fad08be7cfb to your computer and use it in GitHub Desktop.
Save lewang/30b7633586d2fe4a56673fad08be7cfb to your computer and use it in GitHub Desktop.
(ns anki
"Some anki manipulation CSV handling."
(:require [clojure.data.csv :as csv]
[clojure.java.io :as io]
clojure.string))
(defn times-table-rows []
(let [tuples (for [i (range 2 10)
j (range 2 10)
:when (>= j i)]
[(conj (str i"x"j)) i j])
add-product (fn [[_ f1 f2 :as tuple]] (conj tuple (* f1 f2)))
;; add-is-square (fn [[_ f1 f2 :as tuple]] (conj tuple (when (= f1 f2)
;; "square")))
add-tags (fn [[_ f1 f2 :as tuple]]
(conj tuple
(clojure.string/join " "
(cond-> ["times_table" (str "factor" f1)]
(= f1 f2) (conj "square")
(not= f1 f2) (conj (str "factor" f2))))))
add-cloze-data (fn [[_ f1 f2 product :as tuple]]
(conj tuple
(if (= f1 f2)
(str f1 " X " "{{c1::" f1 "}}")
(str "{{c1::" f1 "}}"" X " "{{c2::" f2 "}}"))))]
(->> tuples
(map add-product)
;; (map add-is-square)
(map add-cloze-data)
(map add-tags))))
(defn write-times-table [filename]
(with-open [out-file (io/writer filename)]
(csv/write-csv out-file (times-table-rows))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment