/
arithmetic.cljc
92 lines (85 loc) · 3.05 KB
/
arithmetic.cljc
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
(ns garden.arithmetic
"Generic arithmetic operators for computing sums, differences,
products, and quotients between CSSUnits, CSSColors, and numbers."
(:refer-clojure :exclude [+ - * /])
(:require [garden.units :as u :refer [unit?]]
[garden.color :as c :refer [color?]]))
;; The motivation for the functions in this namespace is the
;; contention that working with unit arithmetic functions (`px+`,
;; `px-`, etc.) and color arithmetic functions (`color+`, `color-`,
;; etc.) can at times be a bit unweildly. In some cases it would be
;; nice to have functions which could transparently perform unit and
;; color math without the verbosity.
;; Here, such functions are provided.
;; All operations favor `CSSUnit` and `CSSColor` types and fall back to
;; the standard `clojure.core` arithmetic functions. The preference for
;; this order stems from the assertion that it is far more likely unit
;; arithmetic will be performed in the context of a stylesheet versus
;; color or numeric.
(defn +
"Generic addition operator. Transparently computes the sum of
`CSSUnit`s,`CSSColor`s, and numbers."
([] 0)
([x] x)
([x y]
(cond
(unit? x) ((u/make-unit-adder (:unit x)) x y)
(color? x) (c/color+ x y)
:else (if (or (unit? y) (color? y))
(+ y x)
(clojure.core/+ x y))))
([x y & more]
(reduce + (+ x y) more)))
(defn -
"Generic subtraction operator. Transparently computes the difference
between `CSSUnit`s, `CSSColor`s, and numbers."
([x]
(cond
(unit? x) (update-in x [:magnitude] clojure.core/-)
;; Colors shouldn't have negative semantics.
(color? x) x
:else (clojure.core/- x)))
([x y]
(cond
(unit? x) ((u/make-unit-subtractor (:unit x)) x y)
(color? x) (c/color- x y)
:else (cond
(unit? y) (let [{m :magnitude} y]
(assoc y :magnitude (clojure.core/- x m)))
(color? y) (c/color- x y)
:else (clojure.core/- x y))))
([x y & more]
(reduce - (- x y) more)))
(defn *
"Generic multiplication operation. Transparently computes the product
between `CSSUnit`s, `CSSColor`s, and numbers."
([] 1)
([x] x)
([x y]
(cond
(unit? x) ((u/make-unit-multiplier (:unit x)) x y)
(color? x) (c/color* x y)
:else (if (or (unit? y) (color? y))
(* y x)
(clojure.core/* x y))))
([x y & more]
(reduce * (* x y) more)))
(defn /
"Generic division operation. Transparently computes the quotient
between `CSSUnit`s, `CSSColor`s, and numbers."
([x]
(cond
(unit? x) (update-in x [:magnitude] clojure.core//)
(color? x) (c/color-div x)
:else (clojure.core// x)))
([x y]
(cond
(unit? x) ((u/make-unit-divider (:unit x)) x y)
(color? x) (c/color-div x y)
:else (cond
(unit? y) (let [{m :magnitude} y]
(assoc y :magnitude (clojure.core// x m)))
(color? y) (c/color-div x y)
:else (clojure.core// x y))))
([x y & more]
(reduce / (/ x y) more)))