Skip to content

Instantly share code, notes, and snippets.

@keyvanakbary
Created December 5, 2014 08:30
Show Gist options
  • Save keyvanakbary/df673ea78663b4bb9a08 to your computer and use it in GitHub Desktop.
Save keyvanakbary/df673ea78663b4bb9a08 to your computer and use it in GitHub Desktop.
(ns listy.list)
(defn create []
{:items (sorted-map) :current nil :index 0})
(defn add [l]
(let [idx (inc (:index l))]
(-> l
(assoc-in [:items idx] {:title "" :items (sorted-map)})
(assoc :index idx)
(assoc :current [:items idx]))))
(defn keys->indexes [m]
(into {} (map-indexed (fn [idx itm] [(first itm) idx]) m)))
(defn indexes->keys [m]
(into {} (map-indexed (fn [idx itm] [idx (first itm)]) m)))
(defn prev-key [m k]
((indexes->keys m) (dec ((keys->indexes m) k))))
(defn prev-key-in [m ks]
(let [path (pop ks)
lat (get-in m path)
pk (prev-key lat (last ks))]
(if pk (conj path pk))))
(defn delete [l]
(update-in l (pop (:current l)) dissoc (last (:current l))))
(defn indent [l]
(let [pk (prev-key-in l (:current l))
idx (peek (:current l))
path (into pk [:items idx])]
(if pk
(-> l
(assoc-in path (get-in l (:current l)))
(delete)
(assoc :current path)
)
l)))
(defn select [l path]
(assoc l :current path))
(defn title [l t]
(assoc-in l (conj (:current l) :title) t))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment