Softened tone in science minus science.
author |
Dylan Holmes <ocsenave@gmail.com> |
date |
Sat, 29 Oct 2011 21:18:54 -0500 |
parents |
b4de894a1e2e |
children |
|
rev |
line source |
rlm@2
|
1 (ns mtg.frame)
|
rlm@2
|
2
|
rlm@2
|
3 ;; GENERALLY USEFUL FUNCTIONS
|
rlm@2
|
4
|
rlm@2
|
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]
|
rlm@2
|
6 (reduce #(if ((first %2) x) (conj %1 (second %2))) '() pred-vals)
|
rlm@2
|
7 )
|
rlm@2
|
8 (defn alter-val "Applies f to the current value associated with each key, associating each key with the value returned." [m f & keys]
|
rlm@2
|
9 (map #(assoc m % (f (get m %))) keys))
|
rlm@2
|
10
|
rlm@2
|
11 (defn every-nth "Returns every nth member of coll. If n is not positive, returns an empty list." [n coll]
|
rlm@2
|
12 (if (<= n 0) '()
|
rlm@2
|
13 (take-while (comp not nil?) (map first (iterate #(nthnext % n) coll)))))
|
rlm@2
|
14
|
rlm@2
|
15
|
rlm@2
|
16
|
rlm@2
|
17
|
rlm@2
|
18
|
rlm@2
|
19 ;; FRAME MANIPULATION
|
rlm@2
|
20
|
rlm@2
|
21
|
rlm@2
|
22 (defn conj-key "Adds the xs to the seq associated with the given key." [map key & xs]
|
rlm@2
|
23 (assoc map key (apply conj (get map key []) xs)))
|
rlm@2
|
24
|
rlm@2
|
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."
|
rlm@2
|
26 [frame & kfs]
|
rlm@2
|
27 (let [id (gensym "up_")
|
rlm@2
|
28 keys (every-nth 2 kfs)
|
rlm@2
|
29 fns (every-nth 2 (rest kfs))]
|
rlm@2
|
30
|
rlm@2
|
31 ((reduce comp (map (fn[k f](fn[m](conj-key m k (list id f) )) keys fns))
|
rlm@2
|
32 (conj-key frame :*bindings* (map (fn [k f](list id k)) keys fns))
|
rlm@2
|
33 )
|
rlm@2
|
34 ))
|
rlm@2
|
35
|
rlm@2
|
36
|
rlm@2
|
37
|
rlm@2
|
38
|
rlm@2
|
39 (def *frame* (atom {:*bindings* '()}))
|