Skip to content

Commit

Permalink
Better handling of measurement units across numeric types.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoejp committed Dec 31, 2023
1 parent d5e8aca commit 61e68fc
Show file tree
Hide file tree
Showing 16 changed files with 489 additions and 138 deletions.
2 changes: 1 addition & 1 deletion lux-mode/lux-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ Called by `imenu--generic-function'."
extension
definition
;; ;;;;;;;;;;;;;;;;;;;;;;
"undefined" "symbol"
"undefined"
"for"
"io"
"infix"
Expand Down
12 changes: 5 additions & 7 deletions stdlib/source/injection/lux/data/text.lux
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
[collection
["[0]" list (.use "[1]#[0]" monad)]]]
[math
["[0]" unit]
[number
["[0]" natural]
["[0]" integer]
Expand Down Expand Up @@ -46,14 +47,11 @@
[text Text text.injection]
)

(template.with [<name>]
[(the .public <name>
(template.with [,name]
[(the .public ,name
(Injection Decimal)
(let [suffix (|> +0.0
(of (template.symbol [decimal._] [<name>]) injection)
(text.clip_since 4)
(maybe.else ""))]
(|>> (of (template.symbol [decimal._] [<name>]) injection)
(let [suffix (its unit.#suffix (template.symbol [unit._] [,name]))]
(|>> (of (template.symbol [decimal._] [,name]) injection)
(text.split_by decimal.separator)
(maybe.else ["" ""])
product.left
Expand Down
131 changes: 62 additions & 69 deletions stdlib/source/library/lux/math/number/decimal.lux
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
["[1][0]" integer]
["[1][0]" revolution]
[//
[arithmetic (.only Arithmetic)]]])
[arithmetic (.only Arithmetic)]
["[0]" unit]]])

(the It .Decimal)

Expand All @@ -36,8 +37,8 @@
(the with_template' (.in_module# .prelude .with_template))
(the template#macro (.in_module# .prelude .template#macro))

(with_template' [<name> <value>]
[(the .public <name>
(with_template' [,name <value>]
[(the .public ,name
It
<value>)]

Expand All @@ -63,8 +64,8 @@
(is (Nominal "java.lang.Double"))
(as It))]))

(with_template' [<name> <method>]
[(the .public <name>
(with_template' [,name <method>]
[(the .public ,name
(-> It
It)
(|>> !double
Expand All @@ -90,8 +91,8 @@
!decimal)))

.js
(these (with_template' [<name> <method>]
[(the .public <name>
(these (with_template' [,name <method>]
[(the .public ,name
(-> It
It)
(|>> []
Expand All @@ -114,8 +115,8 @@
(as It (.js_apply# (.js_constant# "Math.pow") [subject param]))))

.python
(these (with_template' [<name> <method>]
[(the .public <name>
(these (with_template' [,name <method>]
[(the .public ,name
(-> It
It)
(|>> []
Expand Down Expand Up @@ -148,8 +149,8 @@
(..pow (.f64_/# +3.0 +1.0))))))

.lua
(these (with_template' [<name> <method>]
[(the .public <name>
(these (with_template' [,name <method>]
[(the .public ,name
(-> It
It)
(|>> []
Expand Down Expand Up @@ -182,8 +183,8 @@
(..pow (.f64_/# +3.0 +1.0))))))

.ruby
(these (with_template' [<name> <method>]
[(the .public <name>
(these (with_template' [,name <method>]
[(the .public ,name
(-> It
It)
(|>> []
Expand All @@ -197,8 +198,8 @@
[root_3 "Math.cbrt"]
)

(with_template' [<name> <method>]
[(the .public (<name> it)
(with_template' [,name <method>]
[(the .public (,name it)
(-> It
It)
(|> (.ruby_object_do# <method> it [])
Expand All @@ -215,8 +216,8 @@
(as It (.ruby_object_do# "**" subject [param]))))

.php
(these (with_template' [<name> <method>]
[(the .public <name>
(these (with_template' [,name <method>]
[(the .public ,name
(-> It
It)
(|>> ("php apply" ("php constant" <method>))
Expand All @@ -242,8 +243,8 @@
(..pow (.f64_/# +3.0 +1.0))))

.scheme
(these (with_template' [<name> <method>]
[(the .public <name>
(these (with_template' [,name <method>]
[(the .public ,name
(-> It
It)
(|>> ("scheme apply" ("scheme constant" <method>))
Expand Down Expand Up @@ -298,8 +299,8 @@
(again (//natural.* it acc) (-- it))
acc)))

(with_template' [<name> <op>]
[(the .public (<name> param subject)
(with_template' [,name <op>]
[(the .public (,name param subject)
(-> It It
Bit)
(<op> param subject))]
Expand All @@ -325,8 +326,8 @@
(or (.f64_<# it reference)
(.f64_=# it reference)))

(with_template' [<comparison> <name>]
[(the .public <name>
(with_template' [<comparison> ,name]
[(the .public ,name
(Predicate It)
(<comparison> +0.0))]

Expand All @@ -335,8 +336,8 @@
[..= zero?]
)

(with_template' [<name> <op>]
[(the .public (<name> param subject)
(with_template' [,name <op>]
[(the .public (,name param subject)
(-> It It
It)
(<op> param subject))]
Expand Down Expand Up @@ -389,8 +390,8 @@
(the min_exponent -1022)
(the max_exponent (//integer.decimal +1023))

(with_template' [<name> <test>]
[(the .public (<name> left right)
(with_template' [,name <test>]
[(the .public (,name left right)
(-> It It
It)
(if (<test> right left)
Expand Down Expand Up @@ -428,8 +429,8 @@
.f64_int#
(.i64_left# ..exponent_size)))

(with_template' [<name> <numerator>]
[(the .public <name>
(with_template' [,name <numerator>]
[(the .public ,name
It
(../ +0.0 <numerator>))]

Expand Down Expand Up @@ -480,12 +481,12 @@
(..- f2^-52)
(..* f2^+1023))))

(with_template' [<name> <composite> <identity>]
[(the .public <name>
(with_template' [,name ,composite ,identity]
[(the .public ,name
(Monoid It)
(implementation
(the identity <identity>)
(the composite <composite>)))]
(the identity ,identity)
(the composite ,composite)))]

[addition ..+ +0.0]
[multiplication ..* +1.0]
Expand All @@ -496,7 +497,7 @@
(the .public cannot_project
Error
(location.with (location.here)
"Cannot decode."))
"Cannot project."))

(the (decimal#injection it)
(-> It
Expand Down Expand Up @@ -605,8 +606,8 @@
(the exponent_offset ..mantissa_size)
(the sign_offset (//natural.+ ..exponent_size ..exponent_offset))

(with_template' [<cast> <hex> <name>]
[(the <name>
(with_template' [<cast> <hex> ,name]
[(the ,name
(|> <hex>
(of //natural.hex projection)
try.trusted
Expand Down Expand Up @@ -840,49 +841,41 @@
(..+ divisor remainder)
remainder)))

(with_template' [<factor> <suffix> <format> <as> <of>]
(with_template' [<format> <as> <of> ,unit]
[(the .public <as>
(-> It
It)
(* <factor>))
(* (//natural.decimal (its unit.#factor ,unit))))

(the .public <of>
(-> It
It)
(/ <factor>))
(/ (//natural.decimal (its unit.#factor ,unit))))

(the .public <format>
(Format Text It)
(implementation
(the injection
(|>> <as>
(of ..base_10 injection)
(text.suffix <suffix>)))

(the (projection it)
(when (text.split_by <suffix> it)
{.#Some [it ""]}
(|> it
(of ..base_10 projection)
(try#each <of>))

_
{try.#Failure ..cannot_project}))))]

... https://en.wikipedia.org/wiki/Degree_(angle)
[+360.0 "°" degree as_degree of_degree]

... https://en.wikipedia.org/wiki/Gradian
[+400.0 "g" gradian as_gradian of_gradian]

... https://en.wikipedia.org/wiki/Percentage
[+00,100.0 "%" percentage as_percentage of_percentage]

... https://en.wikipedia.org/wiki/Per_mille
[+01,000.0 "‰" permille as_permille of_permille]

... https://en.wikipedia.org/wiki/Basis_point
[+10,000.0 "‱" permyriad as_permyriad of_permyriad]
(let [suffix (its unit.#suffix ,unit)]
(implementation
(the injection
(|>> <as>
(of ..base_10 injection)
(text.suffix suffix)))

(the (projection it)
(when (text.split_by suffix it)
{.#Some [it ""]}
(|> it
(of ..base_10 projection)
(try#each <of>))

_
{try.#Failure ..cannot_project})))))]

[degree as_degree of_degree unit.degree]
[gradian as_gradian of_gradian unit.gradian]
[percentage as_percentage of_percentage unit.percentage]
[permille as_permille of_permille unit.permille]
[permyriad as_permyriad of_permyriad unit.permyriad]
)

(alias [base_02] ..binary)
Expand Down
Loading

0 comments on commit 61e68fc

Please sign in to comment.