aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/generic/arithmetic.clj
blob: 558ec37bfe47effe9d8c16b15463c4eca435fce5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
;; Generic interfaces for arithmetic operations

;; by Konrad Hinsen
;; last updated March 12, 2009

;; Copyright (c) Konrad Hinsen, 2009. All rights reserved.  The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
;; distribution.  By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license.  You must not
;; remove this notice, or any other, from this software.

(ns clojure.contrib.generic.arithmetic
  "Generic arithmetic interface

   NOTE: This library is VERY experimental. It WILL change significantly
   with future release.

   This library defines generic versions of + - * / as multimethods
   that can be defined for any type. The minimal required implementations
   for a type are binary + and * plus unary - and /. Everything else
   is derived from these automatically. Explicit binary definitions
   for - and / can be given for efficiency reasons."
  (:refer-clojure :exclude [+ - * /]))

;
; A dispatch function that separates nulary, unary, binary, and
; higher arity calls and also selects on type for unary and binary
; calls.
;
(defn- nary-dispatch
  ([] ::nulary)
  ([x] [::unary (type x)])
  ([x y]
     (let [tx (type x)
	   ty (type y)]
       (cond (isa? tx ty) [::binary ty]
	     (isa? ty tx) [::binary tx]
	     ; Should there be clause checking if tx and ty have
	     ; a common ancestor? This would cover cases like
	     ; java.lang.Integer and java.lang.Double, which have the
	     ; common ancestor java.lang.Number.
	     :else [::binary tx ty])))
  ([x y & more] ::n-ary))

;
; We can't use [::binary :default], so we need to define a root type
; of the type hierarcy. The derivation for Object covers all classes,
; but all non-class types will need an explicit derive clause.
; Ultimately, a macro might take care of this.
;
(derive Object ::any)

;
; Addition
;
; The minimal implementation is for [::binary my-type]. It is possible
; in principle to implement [::unary my-type] as well, though this
; doesn't make any sense.
;
(defmulti + nary-dispatch)

(defmethod + ::nulary
  []
  ::zero)

(defmethod + [::unary ::any]
  [x] x)

(defmethod + ::n-ary
  [x y & more]
  (if more
    (recur (+ x y) (first more) (next more))
    (+ x y)))

;
; Subtraction
;
; The minimal implementation is for [::unary my-type]. A default binary
; implementation is provided as (+ x (- y)), but it is possible to
; implement [::unary my-type] explicitly for efficiency reasons.
;
(defmulti - nary-dispatch)

(defmethod - ::nulary
  []
  (throw (java.lang.IllegalArgumentException.
	  "Wrong number of arguments passed")))

(defmethod - [::binary ::any]
  [x y] (+ x (- y)))

(defmethod - ::n-ary
  [x y & more]
  (if more
    (recur (- x y) (first more) (next more))
    (- x y)))

;
; Multiplication
;
; The minimal implementation is for [::binary my-type]. It is possible
; in principle to implement [::unary my-type] as well, though this
; doesn't make any sense.
;
(defmulti * nary-dispatch)

(defmethod * ::nulary
  []
  ::one)

(defmethod * [::unary ::any]
  [x] x)

(defmethod * ::n-ary
  [x y & more]
  (if more
    (recur (* x y) (first more) (next more))
    (* x y)))

;
; Division
;
; The minimal implementation is for [::unary my-type]. A default binary
; implementation is provided as (* x (/ y)), but it is possible to
; implement [::unary my-type] explicitly for efficiency reasons.
;
(defmulti / nary-dispatch)

(defmethod / ::nulary
  []
  (throw (java.lang.IllegalArgumentException.
	  "Wrong number of arguments passed")))

(defmethod / [::binary ::any]
  [x y] (* x (/ y)))

(defmethod / ::n-ary
  [x y & more]
  (if more
    (recur (/ x y) (first more) (next more))
    (/ x y)))

;
; Minimal implementations for java.lang.Number
;
(defmethod + [::binary java.lang.Number]
  [x y] (clojure.core/+ x y))

(defmethod - [::unary java.lang.Number]
  [x] (clojure.core/- x))

(defmethod * [::binary java.lang.Number]
  [x y] (clojure.core/* x y))

(defmethod / [::unary java.lang.Number]
  [x] (clojure.core// x))