Skip to content

Instantly share code, notes, and snippets.

@pbostrom
pbostrom / repl.txt
Last active December 28, 2015 04:09
Clojail exception
; nrepl.el 0.2.0 (Clojure 1.5.1, nREPL 0.2.1)
user> (use 'clojail.testers)
nil
user> (use 'clojail.core)
nil
user> (def mysb (sandbox secure-tester-without-def))
#'user/mysb
user> (mysb '(range 10))
(0 1 2 3 4 5 6 7 8 9)
user> (mysb '(range count))
@pbostrom
pbostrom / gist:6926519
Created October 10, 2013 22:20
max sum problem
(defn f [{:keys [mx acc]} x]
{:mx (max mx (+ acc x))
:acc (max 0 (+ acc x))})
(defn max-sum [v]
(:mx (reduce f {:mx 0 :acc 0} v)))
(max-sum [-1, 5, 6, -2, 20, -50, 4]) ;29
(max-sum [-1, 5, 6, -2, 21, -50, 4, -100, 30, -100, 5, 8, -2, 20, -50, 4]) ;31
(max-sum [-2, 1, -3, 4, -1, 2, 1, -5, 4]) ;6
@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)))
@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}})
(defn lame[])
@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]
@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 / 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]
(defn hello [x]
(println "Hello" x))
(hello "name")
@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