This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
forked from shopsmart/clj-foundation
/
math.clj
50 lines (38 loc) · 1.65 KB
/
math.clj
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
(ns clj-foundation.math
"Math abstractions. Currently defines a protocol and type for mixed numbers. Provides a sane
replacement for clojure.core.rationalize! that always returns a rational number.
MixedNumber can then more rationally render Clojure's Rational type as strings, but can also be
used to decompose decimals or rationals > 1 into mixed numbers with easy access to the whole and
fractional parts."
(require [clj-foundation.patterns :refer [let-map]]
[schema.core :as s :refer [=> =>* defschema]])
(import [clojure.lang Ratio Numbers])
(:gen-class))
(defprotocol INumberParts
"A protocol for numbers that can be split into parts."
(decompose [this] "Returns a map consisting of the parts that make up 'this' number."))
(defschema MixedNumberParts
"Formally define a MixedNumber's Map representation as returned by the (.decompose num)
method."
{:whole s/Num
:frac s/Num})
(s/defn rationalize! :- Ratio
"Like clojure.core.rationalize, but always returns a Ratio, even if the number can be reduced to n/1
See also: http://dev.clojure.org/jira/browse/CLJ-1435?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel"
[n :- s/Num]
(Numbers/toRatio (rationalize n)))
(deftype MixedNumber [number]
INumberParts
(decompose [this]
(let [r (rationalize! number)
n (numerator r)
d (denominator r)]
(if (> n d)
(let-map
[whole (long (/ n d))
frac (/ (- n (* whole d)) d)])
{:whole 0
:frac r})))
(toString [this]
(let [mixed-number-parts (.decompose this)]
(str (:whole mixed-number-parts) " " (:frac mixed-number-parts)))))