view mtg/bk.clj @ 9:23db8b1f0ee7

Softened tone in science minus science.
author Dylan Holmes <ocsenave@gmail.com>
date Sat, 29 Oct 2011 21:18:54 -0500
parents b4de894a1e2e
children
line wrap: on
line source
1 (ns mtg.frame)
3 ;; GENERALLY USEFUL FUNCTIONS
5 (defn assay "Takes x and a series of pred-value pairs. Returns a list of vals for which the corresponding preds are true of x." [x & pred-vals]
6 (reduce #(if ((first %2) x) (conj %1 (second %2))) '() pred-vals)
7 )
8 (defn alter-val "Applies f to the current value associated with each key, associating each key with the value returned." [m f & keys]
9 (map #(assoc m % (f (get m %))) keys))
11 (defn every-nth "Returns every nth member of coll. If n is not positive, returns an empty list." [n coll]
12 (if (<= n 0) '()
13 (take-while (comp not nil?) (map first (iterate #(nthnext % n) coll)))))
19 ;; FRAME MANIPULATION
22 (defn conj-key "Adds the xs to the seq associated with the given key." [map key & xs]
23 (assoc map key (apply conj (get map key []) xs)))
25 (defn update "Takes a frame and a sequence of key-fn pairs. Applies f to the current value associated with key, updating the current value with the result. Frames generate and store a unique id for each call to update."
26 [frame & kfs]
27 (let [id (gensym "up_")
28 keys (every-nth 2 kfs)
29 fns (every-nth 2 (rest kfs))]
31 ((reduce comp (map (fn[k f](fn[m](conj-key m k (list id f) )) keys fns))
32 (conj-key frame :*bindings* (map (fn [k f](list id k)) keys fns))
33 )
34 ))
39 (def *frame* (atom {:*bindings* '()}))