Mercurial > dylan
view mtg/bk.clj @ 11:1f112b4f9e8f tip
Fixed what was baroque.
author | Dylan Holmes <ocsenave@gmail.com> |
---|---|
date | Tue, 01 Nov 2011 02:30:49 -0500 |
parents | b4de894a1e2e |
children |
line wrap: on
line source
1 (ns mtg.frame)3 ;; GENERALLY USEFUL FUNCTIONS5 (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 MANIPULATION22 (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* '()}))