comparison 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
comparison
equal deleted inserted replaced
9:35cf337adfcf 10:ef7dbbd6452c
1 ;; PersistentFnMap.clj: implementation for clojure.contrib.fnmap
2
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.
10
11
12 ;; Thanks to Meikel Brandmeyer for his work on lazymap, which made
13 ;; this implementation easier.
14
15
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]}))
22
23 (defn -init
24 ([theMap] [[] theMap])
25 ([theMap metadata] [[metadata] theMap]))
26
27 (defn create [getter setter]
28 (clojure.contrib.fnmap.PersistentFnMap.
29 {::getter getter ::setter setter}))
30
31 ;; IPersistentMap
32
33 (defn -assoc [this key value]
34 (clojure.contrib.fnmap.PersistentFnMap.
35 ((::setter (. this state)) (. this state) key value)))
36
37 ;; Associative
38
39 (defn- -containsKey [this key]
40 (not (nil? ((::getter (. this state)) this key))))
41
42 (defn- -entryAt [this key]
43 (clojure.lang.MapEntry. key ((::getter (. this state)) (. this state) key)))
44
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)))
51
52 ;; Iterable
53
54 (defn -iterator [this]
55 (.. this state iterator))
56
57 ;; IPersistentCollection
58
59 (defn -count [this]
60 (count (. this state)))
61
62 (defn -seq [this]
63 (seq (. this state)))
64
65 (defn -cons [this that]
66 (.. this state (cons this that)))
67
68 (defn -empty [this]
69 (.. this state empty))
70