Skip to content

Instantly share code, notes, and snippets.

;; this file is a walkthrough of Moustache features, a web framework for Clojure
;; http://github.com/cgrand/moustache/tree/master
;; Moustache allows to declare routes, apply middlewares and dispatch on http methods.
;; Moustache is compatible with all frameworks built on Ring, including Compojure
(ns demo
(:use net.cgrand.moustache)
(:use [ring.adapter.jetty :only [run-jetty]])) ;; hmmm Ring without servlets
(ns fn-pre-post
(:refer-clojure :exclude (defn fn defn-)))
(clojure.core/defn- insert-arg
"Insert arg in the second position of the form cond."
[arg cond]
(cons (first cond) (cons arg (rest cond))))
(clojure.core/defn- parse-params
"Parse a parameter vector, possibly containing nested vectors for
;;; All Kinds of Destructuring ;;;
(let [foo 1] foo)
; => 1
(let [[foo bar] [1 2 3]] [foo bar])
; => [1 2]
(let [[foo bar & baz] [1 2 3]] [foo bar baz])
; => [1 2 (3)]
(comment
(:b (DefaultMap. :foo {:a 1}))
; => :foo
(:a (DefaultMap. :foo {:a 1}))
; => 1
(merge-with conj (DefaultMap. [] {}) {:a 1} {:a 2} {:a 3})
; => {:a [1 2 3]}
)
;;; method implementations basically taken from clojure.core/emit-defrecord
(defprotocol Pair (a [_]) (b [_]))
(defmethod print-method user.Pair [o w]
(.write w (str "#<Pair[" (.getName (class o)) "] " (a o) ", " (b o) ">")))
(deftype PairType [a b]
Pair
(a [_] a)
(b [_] b))
(defn some-name [f] ;; Takes a function that
(fn [& fns] ;; Returns a function that takes a list of functions that returns
(fn [& args] ;; A function that calls the original function with identity over applying each function to the args of this anonymous function #ohgod
(f identity (map apply fns (repeat args))))))
(defn itunes [command]
(let [mgr (javax.script.ScriptEngineManager.)
engine (.getEngineByName mgr "AppleScript")]
(.eval engine (str "tell application \"iTunes\" to " command))))
(itunes 'pause)
(itunes 'play)
(itunes 'stop)
;; ...
(defprotocol Spinnable
(spin [this] "Return a seq walking the opposite direction as this"))
(defn iter-bi [x f b]
(reify
Spinnable
(spin [_] (iter-bi x b f))
clojure.lang.ISeq
(first [_] x)
(more [_] (iter-bi (f x) f b))
@RJ
RJ / irccloud_faq.txt
Created October 17, 2010 19:13
Rough cut of FAQ for IRCCloud.com
* Which browsers does it work in?
** We've tested in modern versions of Firefox, Chrome and Safari. It currently doesn't work in IE.
We plan to improve browser support in future.
* Does IRCCloud act as a BNC/bouncer and stay connected when I go offline?
** Yes it does - you will stay connected to IRC even if you shutdown your computer or log out of IRCCloud.com
This means when you come back, you'll be able to see what happened on IRC whilst you were away.
* What will it cost?
** Free currently, will roll out monthly subscription as the beta progresses.
(defn with-default-args [f & default-args]
(fn [& args]
(apply f (->> default-args
(drop (count args))
(concat args)))))
(comment ; usage
(def f (with-default-args + 1 2))