I was trying to experiment with Clojure Protocols and Records recently, and came up with a toy example to clarify my understanding of their usage in the context of developing a simple Stack Abstract Data Type.
For an excellent tutorial on utilizing protocols and records in Clojure btw – check out Kotka.de – Memoize done right .
;; Stack example abstract data type using Clojure protocols and records ;; viksit at gmail dot com ;; 2010 (ns viksit.stack (:refer-clojure :exclude [pop])) (defprotocol PStack "A stack protocol" (push [this val] "Push element into the stack") (pop [this] "Pop element from stack") (top [this] "Get top element from stack")) (defrecord Stack [coll] PStack (push [_ val] (swap! coll conj val)) (pop [_] (let [ret (first @coll)] (swap! coll rest) ret)) (top [_] (first @coll))) ;; Testing stack> (def s (Stack. (atom '()))) #'stack/s stack> (push s 10) (10) stack> (push s 20) (20 10) stack> (top s) 20 stack> s #:stack.Stack{:coll #} stack> (pop s) 20
More tutorial links on Protocols,
[1] http://blog.higher-order.net/2010/05/05/circuitbreaker-clojure-1-2/
[2] http://freegeek.in/blog/2010/05/clojure-protocols-datatypes-a-sneak-peek/
[3] http://groups.google.com/group/clojure/browse_thread/thread/b8620db0b7424712