This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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#))))] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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.)] |