annotate src/clojure/contrib/fnmap/PersistentFnMap.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 ;; PersistentFnMap.clj: implementation for clojure.contrib.fnmap
rlm@10 2
rlm@10 3 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
rlm@10 4 ;; and distribution terms for this software are covered by the Eclipse
rlm@10 5 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 6 ;; which can be found in the file epl-v10.html at the root of this
rlm@10 7 ;; distribution. By using this software in any fashion, you are
rlm@10 8 ;; agreeing to be bound by the terms of this license. You must not
rlm@10 9 ;; remove this notice, or any other, from this software.
rlm@10 10
rlm@10 11
rlm@10 12 ;; Thanks to Meikel Brandmeyer for his work on lazymap, which made
rlm@10 13 ;; this implementation easier.
rlm@10 14
rlm@10 15
rlm@10 16 (ns clojure.contrib.fnmap.PersistentFnMap
rlm@10 17 (:gen-class :extends clojure.lang.APersistentMap
rlm@10 18 :state state
rlm@10 19 :init init
rlm@10 20 :constructors {[clojure.lang.IPersistentMap] [],
rlm@10 21 [clojure.lang.IPersistentMap clojure.lang.IPersistentMap] [clojure.lang.IPersistentMap]}))
rlm@10 22
rlm@10 23 (defn -init
rlm@10 24 ([theMap] [[] theMap])
rlm@10 25 ([theMap metadata] [[metadata] theMap]))
rlm@10 26
rlm@10 27 (defn create [getter setter]
rlm@10 28 (clojure.contrib.fnmap.PersistentFnMap.
rlm@10 29 {::getter getter ::setter setter}))
rlm@10 30
rlm@10 31 ;; IPersistentMap
rlm@10 32
rlm@10 33 (defn -assoc [this key value]
rlm@10 34 (clojure.contrib.fnmap.PersistentFnMap.
rlm@10 35 ((::setter (. this state)) (. this state) key value)))
rlm@10 36
rlm@10 37 ;; Associative
rlm@10 38
rlm@10 39 (defn- -containsKey [this key]
rlm@10 40 (not (nil? ((::getter (. this state)) this key))))
rlm@10 41
rlm@10 42 (defn- -entryAt [this key]
rlm@10 43 (clojure.lang.MapEntry. key ((::getter (. this state)) (. this state) key)))
rlm@10 44
rlm@10 45 (defn -valAt
rlm@10 46 ([this key]
rlm@10 47 ((::getter (. this state)) (. this state) key))
rlm@10 48 ([this key default]
rlm@10 49 (or ((::getter (. this state)) (. this state) key)
rlm@10 50 default)))
rlm@10 51
rlm@10 52 ;; Iterable
rlm@10 53
rlm@10 54 (defn -iterator [this]
rlm@10 55 (.. this state iterator))
rlm@10 56
rlm@10 57 ;; IPersistentCollection
rlm@10 58
rlm@10 59 (defn -count [this]
rlm@10 60 (count (. this state)))
rlm@10 61
rlm@10 62 (defn -seq [this]
rlm@10 63 (seq (. this state)))
rlm@10 64
rlm@10 65 (defn -cons [this that]
rlm@10 66 (.. this state (cons this that)))
rlm@10 67
rlm@10 68 (defn -empty [this]
rlm@10 69 (.. this state empty))
rlm@10 70