Skip to content

Instantly share code, notes, and snippets.

View ordnungswidrig's full-sized avatar

Philipp Meier ordnungswidrig

View GitHub Profile
@ordnungswidrig
ordnungswidrig / conditionals.clj
Created June 14, 2012 10:18
Variations on clojure conditionals
(ns conditionals
(:use clojure.test
[clojure.core.match :only [match]]))
;; variation on conditionals
(defn test-conditional [f]
(is (= (f :foo-exists) 1))
(is (= (f :bar-is-nil) nil))
(is (= (f :some-default) 3))
(is (= (f :some-other-default) 3)))
@ordnungswidrig
ordnungswidrig / start
Created June 6, 2012 11:30
Openshift start and stop hook for leiningen
#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_INTERNAL_IP:8080
# save as .openshift/action_hooks/start
export HTTP_CLIENT="wget --no-check-certificate -O"
export PORT=$OPENSHIFT_INTERNAL_PORT
export HOST=$OPENSHIFT_INTERNAL_IP
export HOME=$OPENSHIFT_DATA_DIR/home
@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))
@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 / 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 / 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 / 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 / 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 / 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 / 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