view src/clojure/contrib/generic.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 ;; Support code for generic interfaces
3 ;; by Konrad Hinsen
4 ;; last updated May 4, 2009
6 ;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use
7 ;; and distribution terms for this software are covered by the Eclipse
8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
9 ;; which can be found in the file epl-v10.html at the root of this
10 ;; distribution. By using this software in any fashion, you are
11 ;; agreeing to be bound by the terms of this license. You must not
12 ;; remove this notice, or any other, from this software.
14 (ns
15 ^{:author "Konrad Hinsen"
16 :skip-wiki true
17 :doc "Generic interfaces
18 This library provides generic interfaces in the form of
19 multimethods that can be implemented for any type.
20 The interfaces partly duplicate existing non-generic
21 functions in clojure.core (arithmetic, comparison,
22 collections) and partly provide additional functions that
23 can be defined for a wide variety of types (functors, math
24 functions). More functions will be added in the future."}
25 clojure.contrib.generic
26 (:use [clojure.contrib.types :only (defadt)]))
28 ;
29 ; A dispatch function that separates nulary, unary, binary, and
30 ; higher arity calls and also selects on type for unary and binary
31 ; calls.
32 ;
33 (defn nary-dispatch
34 ([] ::nulary)
35 ([x] (type x))
36 ([x y]
37 [(type x) (type y)])
38 ([x y & more] ::nary))
40 ;
41 ; We can't use [::binary :default], so we need to define a root type
42 ; of the type hierarcy. The derivation for Object covers all classes,
43 ; but all non-class types will need an explicit derive clause.
44 ; Ultimately, a macro might take care of this.
45 ;
46 (def root-type ::any)
47 (derive Object root-type)
49 ;
50 ; Symbols referring to ::nulary and ::n-ary
51 ;
52 (def nulary-type ::nulary)
53 (def nary-type ::nary)