aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/finance/interest/rate.lux
blob: 61fd9f9a5e48875938f2d6b4879b0d5766952bca (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
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   [abstract
    [monoid (.only Monoid)]]
   [control
    [function
     [predicate (.only Predicate)]]]
   [data
    [text
     ["%" \\format]]]
   [math
    [number
     ["n" nat]
     ["f" frac]]]]]
 [///
  ["[0]" money (.only Money)]])

... https://en.wikipedia.org/wiki/Interest_rate
(type .public Rate
  Frac)

... https://en.wikipedia.org/wiki/Break-even
(def .public break_even
  Rate
  +1.0)

... https://en.wikipedia.org/wiki/Compound_interest
(def .public compound
  (-> Rate Rate
      Rate)
  f.*)

(with_template [<order> <name>]
  [(def .public <name>
     (Predicate Rate)
     (<order> ..break_even))]

  [f.< loss?]
  [f.> gain?]
  [f.= break_even?]
  )

(def .public monoid
  (Monoid Rate)
  (implementation
   (def identity ..break_even)
   (def composite ..compound)))

(def .public format
  (%.Format Rate)
  (|>> (f.- ..break_even)
       %.percentage))

(def .public (rate before after)
  (All (_ $)
    (-> (Money $) (Money $)
        Rate))
  (f./ (n.frac (money.amount before))
       (n.frac (money.amount after))))