Skip to content

Instantly share code, notes, and snippets.

@pbostrom
pbostrom / sieve.clj
Created July 9, 2012 02:39
Clojure Sieve of Eratosthenes - Christophe Grand
(defn primes3 [max]
(let [enqueue (fn [sieve n factor]
(let [m (+ n (+ factor factor))]
(if (sieve m)
(recur sieve m factor)
(assoc sieve m factor))))
next-sieve (fn [sieve candidate]
(if-let [factor (sieve candidate)]
(-> sieve
(dissoc candidate)
@pbostrom
pbostrom / gist:3250162
Created August 3, 2012 18:21
Transaction side effects
(def x (ref 0))
(def a (agent nil))
(defn alter-and-send-side-effects
"Alters refs then sends println actions to agent with new values"
[]
(dosync
(let [newx (alter x inc)])
(send a (fn [_] (println "x is" newx)))))
@pbostrom
pbostrom / gist:3251454
Created August 3, 2012 20:56 — forked from llasram/gist:3251293
Transaction side effects
(let [q (ref []), x (ref 0)]
(defn run-side-effects [_]
(let [fs (dosync
(let [q' @q]
(ref-set q [])
q'))]
(doseq [f fs] (f))))
(defn alter-and-order-side-effects []
(dosync
(defn hello [x]
(println "Hello" x))
(hello "name")
@pbostrom
pbostrom / sum-list.clj
Last active December 13, 2015 21:58 — forked from swannodette/notes.clj
Generates lists of size M containing natural numbers which add up to N (forked from David Nolen with some tweaks to run on cwo.io)
(require '[clojure.core.logic :refer :all])
(require '[clojure.core.logic.fd :as fd])
(defn sumo
([l n] (sumo l 0 n))
([l acc n]
(matche [l]
([[]] (fd/== acc n))
([[x . r]]
(fresh [nacc]
@pbostrom
pbostrom / zebra.clj
Last active December 15, 2015 22:29
(require '[clojure.core.logic :refer :all])
(require '[clojure.tools.macro :as macro])
(defne righto [x y l]
([_ _ [x y . ?r]])
([_ _ [_ . ?r]] (righto x y ?r)))
(defn nexto [x y l]
(conde
((righto x y l))
@pbostrom
pbostrom / gist:5342129
Last active December 15, 2015 23:39 — forked from swannodette/gist:3217582
Sudoku solver via https://gist.github.com/swannodette/3217582, modified for Twitter REPL
;; works with core.logic 0.8.4
(require '[clojure.core.logic :refer :all])
(require '[clojure.core.logic.fd :as fd])
(defn get-square [rows x y]
(for [x (range x (+ x 3))
y (range y (+ y 3))]
(get-in rows [x y])))
(defn init [vars hints]
(defn lame[])
@pbostrom
pbostrom / gist:6391396
Last active December 22, 2015 00:48
Football rankings
(def m
[{:home "Manchester United" :away "Manchester City" :home_score 1 :away_score 0}
{:home "Manchester United" :away "Manchester City" :home_score 2 :away_score 0}
{:home "Manchester City" :away "Arsenal" :home_score 1 :away_score 1}])
(def teams
{"Manchester United" {:points 1200}
"Manchester City" {:points 1200}
"Arsenal" {:points 1200}})
@pbostrom
pbostrom / gist:6471839
Created September 7, 2013 00:51
Paredit for JSON
(add-hook 'js-mode-hook
(lambda ()
(paredit-mode 1)
(local-set-key "{" 'paredit-open-curly)
(local-set-key "}" 'paredit-close-curly)
(local-set-key "\M-{" 'paredit-wrap-curly)
(local-set-key "\M-}" 'paredit-close-curly-and-newline)
(local-set-key "\M-[" 'paredit-wrap-square)
(local-set-key "\M-]" 'paredit-close-square-and-newline)))