Skip to content

Instantly share code, notes, and snippets.

;; Simple example for generating Pascal's triangle
;; using chouser's finger-tree
(use 'clojure.data.finger-tree
'clojure.pprint)
(def pascal
(iterate #(into (double-list)
(map +
(conjr % 0)
(defmacro check-let [b-vec & body]
(if-not (empty? b-vec)
(let [v (take 3 b-vec)
r (apply vector (drop 3 b-vec))]
`(if-let [~@(take 2 v)]
(check-let ~r ~@body)
~(nth v 2)))
`(do ~@body)))
;; turns this:
@sunilnandihalli
sunilnandihalli / curry.clj
Created December 17, 2010 20:33
a macro to create fixed-arity curryable function in clojure
(defmacro def-curry-fn [name args & body]
{:pre [(not-any? #{'&} args)]}
(if (empty? args)
`(defn ~name ~args ~@body)
(let [rec-funcs (reduce (fn [l v]
`(letfn [(helper#
([] helper#)
([x#] (let [~v x#] ~l))
([x# & rest#] (let [~v x#]
(apply (helper# x#) rest#))))]
@rlm
rlm / gist:746185
Created December 18, 2010 05:19
curry.clj
(ns sunil.curry)
(defn partial+
"Takes a function f and fewer than the normal arguments to f, and
returns a fn that takes a variable number of additional args. When
called, the returned function calls f with args + additional args.
differs from the core version in that it works on just one argument."
{:added "1.0"}
([f] f)
([f arg1]
; The New Year in Snowflakes
; A Clojure doodle by Chouser
(import '(java.awt Color Graphics Frame RenderingHints))
(defonce draw-agent (agent nil))
(defonce halt (atom false))
(defmacro ui-thread [& body]
`(javax.swing.SwingUtilities/invokeAndWait (fn [] ~@body)))
@mattpodwysocki
mattpodwysocki / testing-linq.clj
Created January 2, 2011 21:03
Testing LINQ
user=> (import 'System.Linq.Enumerable)
System.Linq.Enumerable
user=> (def r1 (Enumerable/Range 1 10))
#'user/r1
user=> (seq r1)
(1 2 3 4 5 6 7 8 9 10)
user=> (def r2 (. Enumerable (generic Repeat Int32) 3 4))
#'user/r2
user=> (seq r2)
(3 3 3 3)
(defmacro arrow
([val]
val)
([val x & xs]
(if (symbol? x)
(list* 'user/arrow (list x val) xs)
(list* 'user/arrow
(list* (first x) val (rest x))
xs))))
(ns slouchdb)
; Fast scalable in-memory concurrent NoSQL database
(def db (ref {:bob {:age 59 :sex :male}
:bill {:age 17 :sex :male}
:mary {:age 28 :sex :female}}))
;; Views ;;
(defn total-age []
@pepijndevos
pepijndevos / parsistent_heap.clj
Created January 15, 2011 17:13
A persistent heap implemented in Clojure
(ns persistent-heap)
(defn swap [heap idx idy]
(assoc heap idx (get heap idy) idy (get heap idx)))
(defn children [idx]
(let [idx (inc (* idx 2))
idy (inc idx)]
[idx idy]))
@budu
budu / gist:782221
Created January 16, 2011 22:32
Prototype a new Marginalia parser
(use '(clojure.contrib [reflect :only [get-field]])
'(clojure [string :only [join replace]]))
(deftype Comment [content])
(defmethod print-method Comment [comment ^String out]
(.write out (str \" (.content comment) \")))
(defn read-comment [reader semicolon]
(let [sb (StringBuilder.)]