aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/types
diff options
context:
space:
mode:
authorKonrad Hinsen <konrad.hinsen@laposte.net>2009-02-26 20:43:13 +0000
committerKonrad Hinsen <konrad.hinsen@laposte.net>2009-02-26 20:43:13 +0000
commitc8bfe846a06b4a3935b5a542e816767e55095d0f (patch)
tree3f5c2ad7d5f7937e65508a37b1abd12b1664b92c /src/clojure/contrib/types
parent9601e6585de31f60e082197e7ce85dcbc82ca0bb (diff)
types: new implementation using vectors with metadata and the new type selector function
Diffstat (limited to 'src/clojure/contrib/types')
-rw-r--r--src/clojure/contrib/types/examples.clj29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/clojure/contrib/types/examples.clj b/src/clojure/contrib/types/examples.clj
index 26421697..4e801f75 100644
--- a/src/clojure/contrib/types/examples.clj
+++ b/src/clojure/contrib/types/examples.clj
@@ -22,10 +22,10 @@
(leaf :c))))
(defn depth
- [#^tree t]
+ [t]
(match t
empty-tree 0
- (leaf n) 1
+ (leaf _) 1
(node l r) (inc (max (depth l) (depth r)))))
(depth empty-tree)
@@ -34,7 +34,7 @@
;
; Algebraic data types with multimethods: Haskell-style functors
;
-(defmulti fmap (fn [f s] (class s)))
+(defmulti fmap (fn [f s] (type s)))
; Sequences
(defmethod fmap clojure.lang.ISeq
@@ -46,6 +46,11 @@
[f v]
(into [] (map f v)))
+; Maps
+(defmethod fmap clojure.lang.IPersistentMap
+ [f m]
+ (into {} (for [[k v] m] [k (f v)])))
+
; Trees
(defmethod fmap tree
[f t]
@@ -56,6 +61,7 @@
(fmap str '(:a :b :c))
(fmap str [:a :b :c])
+(fmap str {:a 1 :b 2 :c 3})
(fmap str a-tree)
;
@@ -79,3 +85,20 @@
(foo-to-int (bar 3 3 1))
(foo-to-int (bar 0 3 1))
(foo-to-int (bar 10 20 30))
+
+;
+; Value accessors are defined only for algebraic data types that have
+; exactly one constructor. get-values is defined if there is at least
+; one argument in the constructor; it returns a vector of values.
+; get-value is defined only for exactly one argument, it returns
+; the value directly.
+;
+
+(get-value (bar 1 2 3)) ; fails
+(get-values (bar 1 2 3))
+
+(deftype sum-type
+ (sum x))
+
+(get-value (sum 42))
+(get-values (sum 42))