diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/generic.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,54 @@
     1.4 +;; Support code for generic interfaces
     1.5 +
     1.6 +;; by Konrad Hinsen
     1.7 +;; last updated May 4, 2009
     1.8 +
     1.9 +;; Copyright (c) Konrad Hinsen, 2009. All rights reserved.  The use
    1.10 +;; and distribution terms for this software are covered by the Eclipse
    1.11 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
    1.12 +;; which can be found in the file epl-v10.html at the root of this
    1.13 +;; distribution.  By using this software in any fashion, you are
    1.14 +;; agreeing to be bound by the terms of this license.  You must not
    1.15 +;; remove this notice, or any other, from this software.
    1.16 +
    1.17 +(ns
    1.18 +  ^{:author "Konrad Hinsen"
    1.19 +     :skip-wiki true
    1.20 +     :doc "Generic interfaces
    1.21 +           This library provides generic interfaces in the form of
    1.22 +           multimethods that can be implemented for any type.
    1.23 +           The interfaces partly duplicate existing non-generic
    1.24 +           functions in clojure.core (arithmetic, comparison,
    1.25 +           collections) and partly provide additional functions that
    1.26 +           can be defined for a wide variety of types (functors, math
    1.27 +           functions). More functions will be added in the future."}
    1.28 +  clojure.contrib.generic
    1.29 +  (:use [clojure.contrib.types :only (defadt)]))
    1.30 +
    1.31 +;
    1.32 +; A dispatch function that separates nulary, unary, binary, and
    1.33 +; higher arity calls and also selects on type for unary and binary
    1.34 +; calls.
    1.35 +;
    1.36 +(defn nary-dispatch
    1.37 +  ([] ::nulary)
    1.38 +  ([x] (type x))
    1.39 +  ([x y]
    1.40 +     [(type x) (type y)])
    1.41 +  ([x y & more] ::nary))
    1.42 +
    1.43 +;
    1.44 +; We can't use [::binary :default], so we need to define a root type
    1.45 +; of the type hierarcy. The derivation for Object covers all classes,
    1.46 +; but all non-class types will need an explicit derive clause.
    1.47 +; Ultimately, a macro might take care of this.
    1.48 +;
    1.49 +(def root-type ::any)
    1.50 +(derive Object root-type)
    1.51 +
    1.52 +;
    1.53 +; Symbols referring to ::nulary and ::n-ary
    1.54 +;
    1.55 +(def nulary-type ::nulary)
    1.56 +(def nary-type ::nary)
    1.57 +