Skip to content

Instantly share code, notes, and snippets.

View martintrojer's full-sized avatar
🕺
My hovercraft is full of eels

Martin Trojer martintrojer

🕺
My hovercraft is full of eels
View GitHub Profile
@martintrojer
martintrojer / nqueenso.clj
Created July 11, 2012 06:43
queens blog, part 2
(defne nqueenso [l n]
([() _]) ;; queen list empty, s#
([[[x y] . t] _] ;; match/destruct head.tail, ignore n
(nqueenso t n)
(membero x (range n))
(safeo [x y] t)))
(def subo
(fn ([x y r]
(conde
[(== x 0) (== y 0) (== r 0)]
[(== x 0) (== y 1) (== r -1)]
[(== x 1) (== y 0) (== r 1)]
[(== x 1) (== y 1) (== r 0)]))))
@martintrojer
martintrojer / gist:3055248
Created July 5, 2012 17:59 — forked from stuarthalloway/gist:2645453
Datomic queries against Clojure collections
;; Datomic example code
(use '[datomic.api :only (db q) :as d])
;; ?answer binds a scalar
(q '[:find ?answer :in ?answer]
42)
;; of course you can bind more than one of anything
(q '[:find ?last ?first :in ?last ?first]
"Doe" "John")
(ns tut.hello
(:use [datomic.api :only [db q] :as d]))
(defn add-get-user [uri]
(d/create-database uri)
(let [conn (d/connect uri)
dbase (d/db conn)]
(d/transact conn [{:db/id #db/id [:db.part/user]
:db/doc "Hello World!"}])
(ns logevt-cl
(:refer-clojure :exclude [==])
(:use [clojure.core.logic])
(:import [java.util UUID]))
(defrel id-name ^:index id ^:index name)
(defrel id-time ^:index id ^:index time)
(defrel id-op ^:index id ^:index op)
(defrel id-cat ^:index id ^:index cat)
(defrel id-parent-op ^:index id ^:index parent-op)
(let [id (java.util.UUID/randomUUID)]
(emit-timed {:id id :op :add})
(emit-timed {:id id :cat :backend :op :send})
(emit-timed {:id id :cat :backend :op :ack :parent-op :send})
(emit-timed {:id id :cat :backend :op :sub :parent-op :send})
(emit-timed {:id id :cat :distribution :op :contribute})
(emit-timed {:id id :cat :distribution :op :chain-contribute})
(emit-timed {:id id :cat :distribution :op :chain-event :parent-op :contribute})
(emit-timed {:id id :op :done :parent-op :add})))
@martintrojer
martintrojer / bres.clj
Created June 22, 2012 16:48
Bresenham
(defn draw-line!
"Draws a line (bresenham algorithm)"
[color [c0 r0] [c1 r1] screen]
(let [dc (- c1 c0)
dr (- r1 r0)
derr (Math/abs (/ dr dc))]
(loop [err derr
r r0
c c0]
(when (< c c1)
@martintrojer
martintrojer / string-tests
Created June 21, 2012 07:23
Generative Testing
;; example from ScalaCheck
(ns string-tests
(:use clojure.test.generative))
(defspec startsWith
(partial map identity)
[^string a ^string b]
(assert (.startsWith (str a b) a)))
@martintrojer
martintrojer / bfc.clj
Last active October 6, 2015 01:58
BrainF*ck compiler
;; Alan Dipert BFC
(defn bfc [program]
(let [allowed #{\+ \- \< \> \[ \] \.}
src (->> program (filter allowed)
(interpose \space) (apply str))
fns (zipmap '(- + < > . ?) (repeatedly gensym))]
(letfn [(bfc* [s]
(if (vector? s)
`(while (not (~(fns '?))) ~@(map bfc* s))
(ns monads
(:require clojure.set))
(declare ^:dynamic return
^:dynamic bind)
(defn lift-inc [v]
(return (inc v)))
(defn m-add [mv n]