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
import scala.pickling._
import scala.reflect.runtime.universe._
import scala.util.parsing.json._
import scala.collection.mutable.{StringBuilder, Stack}
package object edn {
implicit val pickleFormat: EdnPickleFormat = new EdnPickleFormat
}
@martintrojer
martintrojer / clojure-match.clj
Created February 26, 2013 08:26 — forked from cgrand/clojure-match.clj
interpreters
(use '[clojure.core.match :only [match]])
(defn evaluate [env [sym x y]]
(match [sym]
['Number] x
['Add] (+ (evaluate env x) (evaluate env y))
['Multiply] (* (evaluate env x) (evaluate env y))
['Variable] (env x)))
(def environment {"a" 3, "b" 4, "c" 5})
(use '[clojure.core.match :only [match]])
(defn evaluate [env [sym x y]]
(match [sym]
['Number] x
['Add] (+ (evaluate env x) (evaluate env y))
['Multiply] (* (evaluate env x) (evaluate env y))
['Variable] (env x)))
(def environment {"a" 3, "b" 4, "c" 5})
@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 / 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 / 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 / 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]
(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 / 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")