mirror of
https://github.com/foo-dogsquared/nixos-config.git
synced 2025-02-12 06:19:00 +00:00
bahaghari/lib: add isEven
and isOdd
for math subset
This commit is contained in:
parent
9c69e03395
commit
d69e61f2a8
@ -18,6 +18,34 @@ rec {
|
|||||||
epsilon = pow 0.1 13;
|
epsilon = pow 0.1 13;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# TODO: We may need to export these functions as a separate Nix library.
|
||||||
|
/* Given a number, check if it's an even number.
|
||||||
|
|
||||||
|
Type: isEven :: Int -> Int
|
||||||
|
|
||||||
|
Example:
|
||||||
|
isEven 10
|
||||||
|
=> true
|
||||||
|
|
||||||
|
isEven 13
|
||||||
|
=> false
|
||||||
|
*/
|
||||||
|
isEven = x:
|
||||||
|
(builtins.bitAnd x 1) == 0;
|
||||||
|
|
||||||
|
/* Given a number, check if it's an odd number.
|
||||||
|
|
||||||
|
Type: isOdd :: Int -> Int
|
||||||
|
|
||||||
|
Example:
|
||||||
|
isOdd 10
|
||||||
|
=> true
|
||||||
|
|
||||||
|
isOdd 13
|
||||||
|
=> false
|
||||||
|
*/
|
||||||
|
isOdd = x: !(isEven x);
|
||||||
|
|
||||||
/* Returns the absolute value of the given number.
|
/* Returns the absolute value of the given number.
|
||||||
|
|
||||||
Type: abs :: Int -> Int
|
Type: abs :: Int -> Int
|
||||||
|
@ -12,6 +12,26 @@ let
|
|||||||
round' = self.math.round' (-10);
|
round' = self.math.round' (-10);
|
||||||
in
|
in
|
||||||
lib.runTests {
|
lib.runTests {
|
||||||
|
testMathIsOdd = {
|
||||||
|
expr = self.math.isOdd 45;
|
||||||
|
expected = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
testMathIsOdd2 = {
|
||||||
|
expr = self.math.isOdd 10;
|
||||||
|
expected = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
testMathIsEven = {
|
||||||
|
expr = self.math.isEven 45;
|
||||||
|
expected = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
testMathIsEven2 = {
|
||||||
|
expr = self.math.isEven 10;
|
||||||
|
expected = true;
|
||||||
|
};
|
||||||
|
|
||||||
testMathPowPositive = {
|
testMathPowPositive = {
|
||||||
expr = self.math.pow 2 8;
|
expr = self.math.pow 2 8;
|
||||||
expected = 256;
|
expected = 256;
|
||||||
|
Loading…
Reference in New Issue
Block a user