aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/finance/interest/rate.lux
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--stdlib/source/library/lux/world/finance/interest/rate.lux63
1 files changed, 63 insertions, 0 deletions
diff --git a/stdlib/source/library/lux/world/finance/interest/rate.lux b/stdlib/source/library/lux/world/finance/interest/rate.lux
new file mode 100644
index 000000000..61fd9f9a5
--- /dev/null
+++ b/stdlib/source/library/lux/world/finance/interest/rate.lux
@@ -0,0 +1,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))))