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 / sud-cl.clj
Created August 1, 2012 18:40 — forked from swannodette/gist:3217582
sudoku_compact.clj
(ns sudoku
(:refer-clojure :exclude [==])
(:use clojure.core.logic))
(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]
(def evil-norvig
[[0 0 0 0 0 6 0 0 0]
[0 5 9 0 0 0 0 0 8]
[2 0 0 0 0 8 0 0 0]
[0 4 5 0 0 0 0 0 0]
[0 0 3 0 0 0 0 0 0]
[0 0 6 0 0 3 0 5 4]
[0 0 0 3 2 5 0 0 6]
(defn distincto [s]
(if (seq s)
(all
(distinctfd (first s))
(distincto (next s)))
s#))
(defn all-infd [xs d]
(if (seq xs)
(all
@martintrojer
martintrojer / sud4-ckanren.clj
Created July 27, 2012 18:36
Sudoku core.logic
(defne all-distinctfd [l]
([()])
([[h . t]]
(distinctfd h)
(all-distinctfd t)))
(run 1 [q]
(fresh [a1 a2 a3 a4
b1 b2 b3 b4
c1 c2 c3 c4
@martintrojer
martintrojer / sud.clj
Created July 27, 2012 16:02
Enumerate Sudoku Solutions
(ns sud
(:require [clojure.set :as s]
[clojure.pprint :as pp]))
(defn possible
"Possible values for a given position"
[[x y] board]
(let [horizontal (set (board x))
vertical (reduce (fn [a c] (conj a (c y))) #{} board)
x' (* (quot x 3) 3)
@martintrojer
martintrojer / fact'.clj
Created July 23, 2012 11:13
Untying the Recursive Knot
(defn fact' [fact n]
(if (= n 0) 1
(* n (fact (dec n)))))
@martintrojer
martintrojer / datalog-queries.clj
Created July 20, 2012 14:23
Datalog in core.logic
(ns clog
(:refer-clojure :exclude [==])
(:use clojure.core.logic))
;; ------------------
(def db
#{
[#uuid "9d6280a3-7043-4e7e-9187-ad800743406a" :last-name "Downey"]
[#uuid "9d6280a3-7043-4e7e-9187-ad800743406a" :street "Hanam"]
@martintrojer
martintrojer / 2dbs-defrels.clj
Created July 16, 2012 17:32
core.logic datomic query blog2
(do
(defrel id-first p1 p2)
(defrel id-email p1 p2)
(defrel id2-email p1 p2)
(defrel id2-height p1 p2)
(fact id-first 1 "John")
(fact id-email 1 "[email protected]")
(fact id-first 2 "Jane")
(fact id-email 2 "[email protected]")
(fact id2-email 100 "[email protected]")
@martintrojer
martintrojer / queries.clj
Created July 16, 2012 12:16
Datomic queries in core.logic
(ns queries
(:refer-clojure :exclude [==])
(:use [clojure.core.logic])
(:use [datomic.api :only [q]]))
(defn query [rule xs]
(let [prule (prep rule)]
(map #(binding-map* prule (prep %)) xs)))
;; ---
@martintrojer
martintrojer / bench.clj
Created July 16, 2012 11:16
core.logic datomic query blog
(defn bench [n f]
(let [rand-str #(str (java.util.UUID/randomUUID))
emails (repeatedly n rand-str)
name-email (reduce (fn [res em]
(conj res (vector (rand-str) (rand-str) em)))
[] emails)
email-height (reduce (fn [res em]
(conj res (vector em (rand-int 100))))
[] emails)]
(time (count (f name-email email-height)))))