Mercurial > lasercutter
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 interfaces3 ;; by Konrad Hinsen4 ;; last updated May 4, 20096 ;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use7 ;; and distribution terms for this software are covered by the Eclipse8 ;; 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 this10 ;; distribution. By using this software in any fashion, you are11 ;; agreeing to be bound by the terms of this license. You must not12 ;; remove this notice, or any other, from this software.14 (ns15 ^{:author "Konrad Hinsen"16 :skip-wiki true17 :doc "Generic interfaces18 This library provides generic interfaces in the form of19 multimethods that can be implemented for any type.20 The interfaces partly duplicate existing non-generic21 functions in clojure.core (arithmetic, comparison,22 collections) and partly provide additional functions that23 can be defined for a wide variety of types (functors, math24 functions). More functions will be added in the future."}25 clojure.contrib.generic26 (:use [clojure.contrib.types :only (defadt)]))28 ;29 ; A dispatch function that separates nulary, unary, binary, and30 ; higher arity calls and also selects on type for unary and binary31 ; calls.32 ;33 (defn nary-dispatch34 ([] ::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 type42 ; 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-ary51 ;52 (def nulary-type ::nulary)53 (def nary-type ::nary)