Skip to content

Instantly share code, notes, and snippets.

View ordnungswidrig's full-sized avatar

Philipp Meier ordnungswidrig

View GitHub Profile
@ordnungswidrig
ordnungswidrig / flog.clj
Created April 20, 2011 12:47
Form logging in clojure
(ns flog)
(defprotocol Log
(append [log expression])
(replay [log]))
(deftype SeqLog [log-ref]
Log
(append [seqlog expression] (alter log-ref conj expression))
(replay [seqlog]
@ordnungswidrig
ordnungswidrig / event.clj
Created April 28, 2011 21:15
Event dispatching and the expression problem
(ns event-expr)
(declare send-event)
(defprotocol PrintEventHandler
(handle-print [this]))
(defprotocol BarEventHandler
(handle-bar [this]))
@ordnungswidrig
ordnungswidrig / reify_generic.clj
Created June 8, 2011 14:06
Reify a protocol by giving a generic implementation function that will be called for all protocol methods. Like a proxy.
(ns reify-generic
"reify a protocol such that every method is delegated to a specified method")
(defmacro reify-generic
"Reify the given protocols. Every method of any protocol is implemented such
that f is called as (apply f protocol-method args)
Example:
(defprotocol Calculator
@ordnungswidrig
ordnungswidrig / state-is-a-fold.clj
Created June 16, 2011 15:50
State is a fold over events
(ns state-is-a-fold
(:use clojure.test))
;;; After all, state is a fold of events. For example let's say the events are a sequence of numbers
;;; and we are folding by addition:
(deftest simple
(let [events [1 5 2 4 3]
state (reduce + events)]
(is (= 15 state))))
@ordnungswidrig
ordnungswidrig / handler-protocol.clj
Created June 22, 2011 22:27
Define events a protocol methods
;; On my journey into event sourcing in clojure I'm trying different approaches
;; of modeling events and event handling in clojure.
;; A little ceremony to pay homage to the gods of clojure:
(ns handler-protocol
(:use clojure.contrib.trace)
(:use clojure.pprint)
(:use clojure.test))
;; My example problem domain for this will be a very simple sketch of twitter.
@ordnungswidrig
ordnungswidrig / example.clj
Created August 5, 2011 13:03
Simple clojure rpc server using read and pr to serialize messages
(def s (start-server 3000 inc false))
;; echo 10\n20\n30 | nc localhost 3000
;; => 11\n21\n31
(stop-server s)
@ordnungswidrig
ordnungswidrig / defrecord_test.clj
Created January 18, 2012 15:12
defrecord should defn ->RecordType earlier
(defprotocol Foo (foo [this]))
(defrecord FooImpl [x] Foo (foo [_] (->FooImpl x)))
;; => Unable to resolve symbol: ->Foo in this context
@ordnungswidrig
ordnungswidrig / actors.clj
Created February 10, 2012 13:39
actors playground
(ns actors)
(defn receive [f]
f)
(defn send_ [f & args]
(dosync (alter f apply args)))
(defn actor [f state]
(ref (f state)))
@ordnungswidrig
ordnungswidrig / live.clj
Created April 17, 2012 15:05
clojure live evaluation test
(ns sandbox.live
(:use [clojure.pprint :only (pprint)] )
(:import [javax.swing JFrame JTextArea JScrollPane]
[java.awt BorderLayout Font]
[java.awt.event WindowAdapter]))
(defn schedule [interval f]
(let [flag (atom nil)]
(.start (Thread. #(loop [] (f)
(Thread/sleep interval)
@ordnungswidrig
ordnungswidrig / dedicated-2.el
Created April 19, 2012 14:25
emacs package do dedicate windows to certain major modes
(provide 'dedicated-2)
(defvar d2-mode-window-map ())
(defvar d2-regexp-window-map ())
(defun buffer-mode (buf)
(with-current-buffer buf major-mode))
(defun d2-get-window-by-mode (buffer)
(let* ((mode (buffer-mode buffer))