diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/fnmap/PersistentFnMap.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,70 @@
     1.4 +;; PersistentFnMap.clj: implementation for clojure.contrib.fnmap
     1.5 +
     1.6 +;; Copyright (c) Stuart Sierra, 2009. All rights reserved.  The use
     1.7 +;; and distribution terms for this software are covered by the Eclipse
     1.8 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.9 +;; which can be found in the file epl-v10.html at the root of this
    1.10 +;; distribution.  By using this software in any fashion, you are
    1.11 +;; agreeing to be bound by the terms of this license.  You must not
    1.12 +;; remove this notice, or any other, from this software.
    1.13 +
    1.14 +
    1.15 +;; Thanks to Meikel Brandmeyer for his work on lazymap, which made
    1.16 +;; this implementation easier.
    1.17 +
    1.18 +
    1.19 +(ns clojure.contrib.fnmap.PersistentFnMap
    1.20 +  (:gen-class :extends clojure.lang.APersistentMap
    1.21 +              :state state
    1.22 +              :init init
    1.23 +              :constructors {[clojure.lang.IPersistentMap] [],
    1.24 +                             [clojure.lang.IPersistentMap clojure.lang.IPersistentMap] [clojure.lang.IPersistentMap]}))
    1.25 +
    1.26 +(defn -init
    1.27 +  ([theMap] [[] theMap])
    1.28 +  ([theMap metadata] [[metadata] theMap]))
    1.29 +
    1.30 +(defn create [getter setter]
    1.31 +  (clojure.contrib.fnmap.PersistentFnMap.
    1.32 +   {::getter getter ::setter setter}))
    1.33 +
    1.34 +;; IPersistentMap
    1.35 +
    1.36 +(defn -assoc [this key value]
    1.37 +  (clojure.contrib.fnmap.PersistentFnMap.
    1.38 +   ((::setter (. this state)) (. this state) key value)))
    1.39 +
    1.40 +;; Associative
    1.41 +
    1.42 +(defn- -containsKey [this key]
    1.43 +  (not (nil? ((::getter (. this state)) this key))))
    1.44 +
    1.45 +(defn- -entryAt [this key]
    1.46 +  (clojure.lang.MapEntry. key ((::getter (. this state)) (. this state) key)))
    1.47 +
    1.48 +(defn -valAt
    1.49 +  ([this key]
    1.50 +     ((::getter (. this state)) (. this state) key))
    1.51 +  ([this key default]
    1.52 +     (or ((::getter (. this state)) (. this state) key)
    1.53 +         default)))
    1.54 +
    1.55 +;; Iterable
    1.56 +
    1.57 +(defn -iterator [this]
    1.58 +  (.. this state iterator))
    1.59 +
    1.60 +;; IPersistentCollection
    1.61 +
    1.62 +(defn -count [this]
    1.63 +  (count (. this state)))
    1.64 +
    1.65 +(defn -seq [this]
    1.66 +  (seq (. this state)))
    1.67 +
    1.68 +(defn -cons [this that]
    1.69 +  (.. this state (cons this that)))
    1.70 +
    1.71 +(defn -empty [this]
    1.72 +  (.. this state empty))
    1.73 +