view 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 source
1 ;; PersistentFnMap.clj: implementation for clojure.contrib.fnmap
3 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
4 ;; and distribution terms for this software are covered by the Eclipse
5 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
6 ;; which can be found in the file epl-v10.html at the root of this
7 ;; distribution. By using this software in any fashion, you are
8 ;; agreeing to be bound by the terms of this license. You must not
9 ;; remove this notice, or any other, from this software.
12 ;; Thanks to Meikel Brandmeyer for his work on lazymap, which made
13 ;; this implementation easier.
16 (ns clojure.contrib.fnmap.PersistentFnMap
17 (:gen-class :extends clojure.lang.APersistentMap
18 :state state
19 :init init
20 :constructors {[clojure.lang.IPersistentMap] [],
21 [clojure.lang.IPersistentMap clojure.lang.IPersistentMap] [clojure.lang.IPersistentMap]}))
23 (defn -init
24 ([theMap] [[] theMap])
25 ([theMap metadata] [[metadata] theMap]))
27 (defn create [getter setter]
28 (clojure.contrib.fnmap.PersistentFnMap.
29 {::getter getter ::setter setter}))
31 ;; IPersistentMap
33 (defn -assoc [this key value]
34 (clojure.contrib.fnmap.PersistentFnMap.
35 ((::setter (. this state)) (. this state) key value)))
37 ;; Associative
39 (defn- -containsKey [this key]
40 (not (nil? ((::getter (. this state)) this key))))
42 (defn- -entryAt [this key]
43 (clojure.lang.MapEntry. key ((::getter (. this state)) (. this state) key)))
45 (defn -valAt
46 ([this key]
47 ((::getter (. this state)) (. this state) key))
48 ([this key default]
49 (or ((::getter (. this state)) (. this state) key)
50 default)))
52 ;; Iterable
54 (defn -iterator [this]
55 (.. this state iterator))
57 ;; IPersistentCollection
59 (defn -count [this]
60 (count (. this state)))
62 (defn -seq [this]
63 (seq (. this state)))
65 (defn -cons [this that]
66 (.. this state (cons this that)))
68 (defn -empty [this]
69 (.. this state empty))