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