Skip to content

Instantly share code, notes, and snippets.

(defn- lookup-edge [edges label]
(get edges label (edges :default)))
(defn- merge-nfa-edges [& edge-maps]
(into {}
(for [label (set (mapcat keys edge-maps))]
[label (apply union
(for [edges edge-maps]
(or (lookup-edge edges label) #{})))])))
@amalloy
amalloy / macroexpand-n.clj
Created December 9, 2011 05:46 — forked from egamble/macroexpand-n.clj
Recursively macroexpand to depth n
(defn macroexpand-n [n form]
(if (zero? n)
form
(recur (dec n)
(macroexpand-1 form))))
@amalloy
amalloy / gist:1420686
Created December 1, 2011 23:29
.ssh/config
Host nfsn
Hostname ssh.phx.nearlyfreespeech.net
Host *
IdentityFile ~/.ssh/id_fibonatch_auto
IdentityFile ~/.ssh/id_fibonatch_weak
(frequencies (for [obj (map (comp coerce-date :created-at) coll)]
(clojure.string/join "/" ((juxt year month) obj))))
@amalloy
amalloy / gist:1346739
Created November 8, 2011 01:15 — forked from CampingScorpion/gist:1346666
first-truthy-fn
;; this works fine
(defn- first-truthy-fn* [[preds & more-preds] args]
(when pred
(if (apply pred args) pred (recur more-preds args))))
(defn first-truthy-fn
"Returns the first function in a seq of functions
that evaluates to truthy for the given arguments -
this shortciruits, only evaluating the functions
(ns shuffle.core
(:use (incanter core charts)))
; naive, O(n+m)
(defn take-rand1 [n coll] (take n (shuffle coll)))
; lazy, O(n!@#$%m^&)
(defn take-rand2 [n coll]
(let [coll (vec coll)]
(take n (distinct (repeatedly #(rand-nth coll))))))
;; amalloy's solution to Love Triangle
;; https://4clojure.com/problem/127
(fn [m t e c i a r v g]
(let [p (t #(<= % (m g))
(i #(* 2 %) 1))
H (c g)
W (c p)
g (v (for [y g]
(v
;; amalloy's solution to Tree reparenting
;; https://4clojure.com/problem/130
(fn [new-root tree]
(let [connections ((fn links [tree]
(when-let [[root & children] (seq tree)]
(let [child-links (apply merge {} (map links children))
conj (fnil conj [])]
(reduce (fn [m [child]]
(-> m
;; amalloy's solution to Simple closures
;; https://4clojure.com/problem/107
{0 {2 1}
1 {2 2}
2 {2 4, 16 256}
3 {1 1, 2 8, 3 27, 4 64}
4 {2 16}
8 {2 256}}
@amalloy
amalloy / gist:1246161
Created September 27, 2011 20:33 — forked from darklajid/gist:1246123
Prime number generator in clojure, port from F#
(defn primes "Generates an (infinite, lazy) sequence of primes"
[]
(letfn [(reinsert [table x prime]
(let [key (+ prime x)]
(update-in table key conj prime)))
(primes-step [table d]
(if-let [factors (get table d)]
(let [new-table (dissoc table d)]
(recur (reduce #(reinsert %1 d %2)
new-table