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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
;; Generic interfaces for arithmetic operations
;; by Konrad Hinsen
;; last updated March 13, 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 provided for efficiency reasons."
(:use [clojure.contrib.types :only (defadt)])
(: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] (type x))
([x y]
[(type x) (type y)])
([x y & more] ::n-ary))
;
; Universal zero and one values
;
(defadt ::zero zero)
(defadt ::one one)
;
; 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)
(derive ::zero ::any)
(derive ::one ::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 + ::any
[x] x)
(defmethod + [::any ::zero]
[x y] x)
(defmethod + [::zero ::any]
[x y] y)
(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 - [::any ::zero]
[x y] x)
(defmethod - [::zero ::any]
[x y] (- y))
(defmethod - [::any ::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 * ::any
[x] x)
(defmethod * [::any ::one]
[x y] x)
(defmethod * [::one ::any]
[x y] y)
(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 / [::any ::one]
[x y] x)
(defmethod / [::one ::any]
[x y] (/ y))
(defmethod / [::any ::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 + [java.lang.Number java.lang.Number]
[x y] (clojure.core/+ x y))
(defmethod - java.lang.Number
[x] (clojure.core/- x))
(defmethod * [java.lang.Number java.lang.Number]
[x y] (clojure.core/* x y))
(defmethod / java.lang.Number
[x] (clojure.core// x))
|