Skip to content

Instantly share code, notes, and snippets.

View ordnungswidrig's full-sized avatar

Philipp Meier ordnungswidrig

View GitHub Profile
(defmulti draw (fn [ shape & rest ] shape))
(defmethod draw ::shape [x y] "shape")
(derive ::shape ::rect)
(defmethod draw ::rect [x y w h] "rect")
(derive ::shape ::eclipse)
(defmethod draw ::rect [x y w h] "eclipse")
(.format (new java.text.SimpleDateFormat "EEE, dd MMM yyyy HH:mm:ss Z" java.util.Locale/US)
(new java.util.Date (+ 3600000 (java.lang.System/currentTimeMillis))))
;; functions under test
(defn xx [a b]
10)
(defn yy [z]
20)
(defn fn-under-test []
(xx 1 2)
(def products-resource
(resource
:method-allowed? #(some #{(% :request-method)} [:get :post])
:content-types-provided { "text/html" :to_html, "text/plain" :to_text }
:created (fn [_ req _] (str "Product " (add-product (slurp-body req)) " created."))
:to_html (fn [_ req _]
(html [:html
[:head [:title "All Products"]]
[:body [:h1 "All Products"]
[:ul (map (fn [p] [:li [:a { :href (p :id)} (p :title)]])
(def hello-resource
(resource
:content-types-provided { "text/html" :to_html ; lookup
"text/markdown" :please_make_markdown ;double lookup
"text/plain" "Plaintext" ; literal
"x/y" (fn [rmap req status] (str "Status " status)) ; function
"foo/bar" [500 "Experimental Server Error"] } ;
:to_html (html [:body [:h1 "This is HTML!"]]
:please_make_markdown :please_please_markdown
:please_please_markdown "Yes, this is markdown"))
(def jabba {:name "Jabba the Hutt"
:age 441
:numbers [1 2 -1]
:location {:planet "Tattoine"}
:foos [{:name "b"} {:name "ab" :size 5}]})
(def expected
{:validator :all,
:level :error,
(def jabba {:name "Jabba the Hutt"
:age 441
:numbers [1 2 -1]
:location {:planet "Tattoine"}
:foos [{:name "b"} {:name "ab" :size 5}]})
(def expected
{:validator :all,
:level :error,
:reason
@ordnungswidrig
ordnungswidrig / aloha.js
Created May 14, 2010 20:11
Simple jquery extension for dom based templating. Or like that.
(function(){
jQuery.fn.tpl = function(f) {
var sTpl = function(f) {
var nodeInDocument = $(this);
var tpl = nodeInDocument.clone();
return {
apply: function(data) {
@ordnungswidrig
ordnungswidrig / alg.clj
Created September 15, 2010 10:32
how can the multiple iteration be simplified?
;; [1 2 3 4] -> [[[] [1 2 3 4]] [[1] [2 3 4]] [[2 3] [3 4]] ... [[1 2 3 4] []]]
(defn sublists [xs]
(map (fn [i] [(take i xs) (drop i xs)])
(range (inc (count xs)))))
(defn gist-me [xs]
(map (fn [[preds succs]]
{:preds (reduce + preds) ;; image costly reduction function
:succs (reduce * (reverse succs))}) ;; imagine costly reduction function
(sublists xs)))
@ordnungswidrig
ordnungswidrig / perm.clj
Created December 14, 2010 22:14
Permutations filtered by predicat
;;; eager permutations
(defn pop-nth [coll i]
[(nth coll i) (concat (take i coll) (drop (inc i) coll))])
(defn perms [items]
(letfn [(p [previous rest]
(if (empty? rest)
[previous]
(mapcat (fn [i]