This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; not sure whether this is an improvement but I don't really like letfn | |
(defn keep-only | |
[n bucket-fn s] | |
((fn my-filter | |
[s seen] | |
(lazy-seq | |
(when (seq s) | |
(let [e (first s) | |
b (bucket-fn e) | |
c (inc (get seen b 0))] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use 'name.choi.joshua.fnparse) | |
(def tokenize (partial re-seq #"[\[\](){}]|[^\[\](){}]+")) | |
(defn initial-state | |
[input] {:remainder (tokenize input) | |
:brackets (list)}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import java.awt.event.ActionListener) | |
(defn add-action [cmp fn] | |
(doto cmp | |
(.addActionListener (doto (proxy [ActionListener] []) | |
(update-proxy {"actionPerformed" fn}))))) | |
(defn add-action [cmp fn] | |
(doto cmp | |
(.addActionListener (reify ActionListener |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro when-lets [bindings & body] | |
(reduce (fn [acc tstform] | |
`(when-let ~tstform ~acc)) | |
(cons 'do body) | |
(reverse (map vec (partition 2 bindings))))) | |
(when-lets [n (first sizes) | |
s (seq coll)] | |
(cons (take n coll) | |
(partitions (next sizes) (drop n coll))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro phase-fn | |
([argvec] identity) | |
([argvec subphase & left] | |
`(fn [session# ~@argvec] | |
(--> session# | |
~subphase | |
~@(when left | |
[`((phase-fn ~argvec ~@left))]))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro defratios | |
"Defines two converter functions with num-expr being how many of a make up b. | |
For example, there are 100 centimeters in a meter. To tell the program to make | |
(centimeter->meter) and (meter->centimeter), use (defratios meter centimeters 100)" | |
[a-symb b-symb num-expr] | |
(and (every? #{clojure.lang.Symbol} | |
(map type [a-symb b-symb])) | |
(let [ab-symb (symbol (arr a-symb b-symb)) | |
ba-symb (symbol (arr b-symb a-symb)) | |
num (gensym "num-")] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn routes | |
"Calculates all the routes from [x y] to [0 0]. | |
Routes move only down and left." | |
[x y] | |
((fn routes* [queue] | |
(lazy-seq | |
(loop [queue queue] | |
(when (pos? (count queue)) | |
(let [[current-level [x y]] (peek queue) | |
queue-left (pop queue) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn my-exists1? [lst obj & {:keys [testfn] :or {testfn =}}] | |
(loop [lst lst, obj obj] | |
(when lst | |
(if (testfn (first lst) obj) | |
lst | |
(recur (next lst) obj))))) | |
(my-exists1? '((1 2) (3 4) (5 6)) 3 :testfn #(= (first %1) %2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn sierpinskisize | |
"Chooses a vertex from tri at random, returning the point halfway between | |
p and the vertex." | |
[p tri] | |
(let [v-num (rand-int 3) | |
vertex ((case v-num | |
0 :p1 | |
1 :p2 | |
2 :p3) tri) | |
halfway-point (fn [p1 p2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def denoms [1000 500 100 25 5 1]) | |
(def names {2000 :twenties | |
1000 :tens | |
500 :fives | |
100 :dollars | |
25 :quarters | |
5 :nickels | |
1 :pennies}) |
OlderNewer