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 / gist:3700747
Created September 11, 2012 18:42 — forked from Chouser/gist:3687532
Lazy seq as event subscription mechanism
;; Here is a spike of a lightweight in-process pubsub mechanism that allows pure ;; functional consumers, both blocking and asynchronous.
;; This defines the event stream, in this case just a series of numbers,
;; a new one produced each second
(defn timer []
(lazy-seq
(do
(Thread/sleep 1000)
(cons (System/nanoTime) (timer)))))
@martintrojer
martintrojer / nqueens-cl.clj
Created September 11, 2012 18:37
even more queens
(defne safeo
"Is the queen q safe from all queens in list"
[q others]
([_ ()])
([[x1 y1] [[x2 y2] . t]]
(!= x1 x2)
(!= y1 y2)
(project [x1 x2 y1 y2]
(!= (- x2 x1) (- y2 y1))
(!= (- x1 y2) (- x2 y1)))
(defn sort-parts
"Lazy, tail-recursive, incremental quicksort. Works against
and creates partitions based on the pivot, defined as 'work'."
[work]
(lazy-seq
(loop [[part & parts] work]
(if-let [[pivot & xs] (seq part)]
(let [smaller? #(< % pivot)]
(recur (list*
(filter smaller? xs)
@martintrojer
martintrojer / tutorial.clj
Created August 27, 2012 14:05
org.clojure/java.jdbc
(ns jdbc.core
(:require [clojure.java.jdbc :as jdbc]))
(def db-spec {:classname "org.sqlite.JDBC"
:subprotocol "sqlite"
:subname "test.db"})
(jdbc/with-connection db-spec
(jdbc/create-table :authors
[:id "integer primary key"]
(defn join-test3 [xs ys]
(let [db-base (make-database
(relation :last-first-email [:last :first :email])
(index :last-first-email :email)
(relation :email-height [:email :height])
(index :email-height :email))
rules (rules-set
(<- (:first-height :first ?f :height ?h)
(:last-first-email :last ?l :first ?f :email ?e)
(:email-height :email ?e :height ?h)))
@martintrojer
martintrojer / gist:3417809
Created August 21, 2012 17:46 — forked from yogthos/gist:3411106
Metaballs with optimizations
(ns metaballs
(:import
[javax.swing JFrame]
[java.awt Canvas Graphics Color]
java.awt.image.BufferStrategy))
(set! *warn-on-reflection* true)
(def ^:const SIZE 250)
(defn direction [p v]
@martintrojer
martintrojer / project.clj
Created August 21, 2012 17:35
A 643 chars minimized self-referential Leiningen project to bootstrap 4k Clojure demos.
(require 'leiningen.core.project)(leiningen.core.project/defproject _"":dependencies[[leiningen-core"2.0.0-preview8"]]:plugins[[lein-swank"1.4.4"]]:main project :source-paths["."])(ns project)(defn -main[](let[f(doto(java.awt.Frame.)(.setUndecorated true))](->(java.awt.GraphicsEnvironment/getLocalGraphicsEnvironment).getDefaultScreenDevice(.setFullScreenWindow f))(.createBufferStrategy f 2)(let[b(.getBufferStrategy f)s(.getBounds f)](while 1(let[g(.getDrawGraphics b)](try(.setColor g(java.awt.Color.(rand-int 0xffffff)))(.fillRect g 0 0(.width s)(.height s))(finally(.dispose g))))(when-not(.contentsLost b)(.show b))(Thread/sleep 20)))))
@martintrojer
martintrojer / rdf-cl.clj
Created August 16, 2012 18:36
RDF graph core.logic
;; http://www.infoq.com/presentations/core-logic
(ns rdf
(:refer-clojure :exclude [==])
(:use [clojure.core.logic]))
(def g
[[:a1 :rdf/type :sdx/FederationService]
[:a1 :rdf/type :sdx/name "movies"]
[:a1 :rdf/exposedServices :_syscoll1]
@martintrojer
martintrojer / gist:3342658
Created August 13, 2012 17:35 — forked from swannodette/gist:3341330
clique.clj
;; http://dosync.posterous.com/know-your-bounds
(defrel connected ^:index x ^:index y)
(facts connected [[1 2] [1 5]])
(facts connected [[2 1] [2 3] [2 5]])
(facts connected [[3 2] [3 4]])
(facts connected [[4 3] [4 5] [4 6]])
(facts connected [[5 1] [5 2] [5 4]])
(facts connected [[6 4]])
@martintrojer
martintrojer / queens.clj
Created August 2, 2012 20:43
nqueens-ckanren
(defn diago [qi qj d rng]
(fresh [qid qjd]
(infd qid qjd rng)
(+fd qi d qid)
(!=fd qid qj)
(+fd qj d qjd)
(!=fd qjd qi)))
(defn diagonalso [n r]
((fn dloop [r i s j]