aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/math/number/int.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/library/lux/math/number/int.lux')
-rw-r--r--stdlib/source/library/lux/math/number/int.lux47
1 files changed, 15 insertions, 32 deletions
diff --git a/stdlib/source/library/lux/math/number/int.lux b/stdlib/source/library/lux/math/number/int.lux
index d5546a4a5..657780bd9 100644
--- a/stdlib/source/library/lux/math/number/int.lux
+++ b/stdlib/source/library/lux/math/number/int.lux
@@ -20,29 +20,24 @@
["#." i64]])
(def: .public (= reference sample)
- {#.doc "Int(eger) equivalence."}
(-> Int Int Bit)
("lux i64 =" reference sample))
(def: .public (< reference sample)
- {#.doc "Int(eger) less-than."}
(-> Int Int Bit)
("lux i64 <" reference sample))
(def: .public (<= reference sample)
- {#.doc "Int(eger) less-than or equal."}
(-> Int Int Bit)
(if ("lux i64 <" reference sample)
#1
("lux i64 =" reference sample)))
(def: .public (> reference sample)
- {#.doc "Int(eger) greater-than."}
(-> Int Int Bit)
("lux i64 <" sample reference))
(def: .public (>= reference sample)
- {#.doc "Int(eger) greater-than or equal."}
(-> Int Int Bit)
(if ("lux i64 <" sample reference)
#1
@@ -60,7 +55,6 @@
(template [<name> <test> <doc>]
[(def: .public (<name> left right)
- {#.doc <doc>}
(-> Int Int Int)
(if (<test> right left)
left
@@ -72,7 +66,6 @@
(template [<name> <op> <doc>]
[(def: .public (<name> param subject)
- {#.doc <doc>}
(-> Int Int Int)
(<op> param subject))]
@@ -84,35 +77,29 @@
)
(def: .public (/% param subject)
- {#.doc "Int(eger) [division remainder]."}
(-> Int Int [Int Int])
[(../ param subject)
(..% param subject)])
-(def: .public (opposite value)
- {#.doc (example "A value of equal magnitude and opposite sign.")}
+(def: .public (opposite it)
(-> Int Int)
- (..- value +0))
+ (..- it +0))
-(def: .public (abs x)
- {#.doc (example "A value of equal magnitude and positive sign.")}
+(def: .public (abs it)
(-> Int Int)
- (if (..< +0 x)
- (..* -1 x)
- x))
+ (if (..< +0 it)
+ (..* -1 it)
+ it))
-(def: .public (signum x)
- {#.doc (example "A value (either -1, 0 or +0) which represents the sign.")}
+(def: .public (signum it)
(-> Int Int)
- (cond (..= +0 x) +0
- (..< +0 x) -1
+ (cond (..= +0 it) +0
+ (..< +0 it) -1
... else
+1))
... https://rob.conery.io/2018/08/21/mod-and-remainder-are-not-the-same/
(def: .public (mod divisor dividend)
- {#.doc (example "Integer modulo."
- "Note: The modulo and the remainder are not the same.")}
(All [m] (-> Int Int Int))
(let [remainder (..% divisor dividend)]
(if (or (and (..< +0 divisor)
@@ -132,7 +119,6 @@
... https://en.wikipedia.org/wiki/Greatest_common_divisor
(def: .public (gcd a b)
- {#.doc "Greatest Common Divisor."}
(-> Int Int Int)
(case b
+0 a
@@ -144,7 +130,6 @@
... https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
(def: .public (extended_gcd a b)
- {#.doc "Extended euclidean algorithm."}
(-> Int Int [[Int Int] Int])
(loop [x +1 x1 +0
y +0 y1 +1
@@ -158,7 +143,6 @@
... https://en.wikipedia.org/wiki/Least_common_multiple
(def: .public (lcm a b)
- {#.doc "Least Common Multiple."}
(-> Int Int Int)
(case [a b]
(^or [_ +0] [+0 _])
@@ -223,25 +207,25 @@
[(implementation: .public <struct>
(Codec Text Int)
- (def: (encode value)
+ (def: (encoded value)
(if (..< +0 value)
- (|> value ++ ..opposite .nat ++ (\ <codec> encode) ("lux text concat" ..-sign))
- (|> value .nat (\ <codec> encode) ("lux text concat" ..+sign))))
+ (|> value ++ ..opposite .nat ++ (\ <codec> encoded) ("lux text concat" ..-sign))
+ (|> value .nat (\ <codec> encoded) ("lux text concat" ..+sign))))
- (def: (decode repr)
+ (def: (decoded repr)
(let [input_size ("lux text size" repr)]
(if (//nat.> 1 input_size)
(case ("lux text clip" 0 1 repr)
(^ (static ..+sign))
(|> repr
("lux text clip" 1 (-- input_size))
- (\ <codec> decode)
+ (\ <codec> decoded)
(\ try.functor map .int))
(^ (static ..-sign))
(|> repr
("lux text clip" 1 (-- input_size))
- (\ <codec> decode)
+ (\ <codec> decoded)
(\ try.functor map (|>> -- .int ..opposite --)))
_
@@ -261,7 +245,6 @@
(def: hash .nat))
(def: .public (right_shifted parameter subject)
- {#.doc "Signed/arithmetic bitwise right-shift."}
(-> Nat Int Int)
(//i64.or (//i64.and //i64.sign subject)
(//i64.right_shifted parameter subject)))