std/int
Functions for Int, the type of whole numbers of any size.
Ints never overflow (§11.2). a / b rounds toward zero, a % b has the
sign of a, and both are 0 when b is 0. The functions divide,
remainder, modulo, and floor_divide give Error(Nil) for 0
instead.
to_string
The number in decimal digits, with - before a negative number.
int.to_string(-42) // "-42" to_string = (x : Int) -> String { core.inspect(x) } parse
Reads a decimal number: an optional - or +, then one or more digits
0 to 9. Any other text gives Error(Nil).
int.parse("-42") // Ok(-42)
int.parse("4.2") // Error(Nil) parse = (text : String) -> Result(Int, Nil) { parse_base(text, 10) } parse_base
Reads a number in the given base, from 2 to 36: an optional - or +,
then one or more digits. Digits after 9 are the letters a to z, in
lower or upper case. A base outside 2 to 36, or any other text, gives
Error(Nil).
int.parse_base("ff", 16) // Ok(255) parse_base = (text : String, base : Int) -> Result(Int, Nil) { to_base
The number in the given base, from 2 to 36, with upper-case letters for
the digits after 9. A base outside 2 to 36 gives Error(Nil).
int.to_base(255, 16) // Ok("FF")
int.to_base(-5, 2) // Ok("-101") to_base = (x : Int, base : Int) -> Result(String, Nil) { to_base2
The number in base 2.
to_base2 = (x : Int) -> String { base_string(x, 2) } to_base8
The number in base 8.
to_base8 = (x : Int) -> String { base_string(x, 8) } to_base16
The number in base 16, with upper-case letters.
to_base16 = (x : Int) -> String { base_string(x, 16) } to_base36
The number in base 36, with upper-case letters.
to_base36 = (x : Int) -> String { base_string(x, 36) } to_float
The nearest Float. A number too big for a Float stops the program.
to_float = (x : Int) -> Float { core.to_float(x) } abs
The number without its sign.
abs = (x : Int) -> Int { { x < 0 } |> (True) { -x } | (False) { x } } negate
The number with the other sign.
negate = (x : Int) -> Int { -x } min
The smaller of two numbers.
min = (a : Int, b : Int) -> Int { { a < b } |> (True) { a } | (False) { b } } max
The greater of two numbers.
max = (a : Int, b : Int) -> Int { { a > b } |> (True) { a } | (False) { b } } clamp
The number, but not less than min and not greater than max. When
min is greater than max, the two bounds are swapped.
int.clamp(40, 50, 60) // 50 clamp = (x : Int, min : Int, max : Int) -> Int { compare
Compares two numbers.
int.compare(1, 2) // Less compare = (a : Int, with : Int) -> Order { is_even
True when the number is a multiple of 2.
is_even = (x : Int) -> Bool { x % 2 == 0 } is_odd
True when the number is not a multiple of 2.
is_odd = (x : Int) -> Bool { x % 2 != 0 } power
base multiplied by itself of times. A negative exponent gives
Error(Nil), because the result would not be a whole number.
int.power(2, 10) // Ok(1024) power = (base : Int, of : Int) -> Result(Int, Nil) { square_root
The square root as a Float. A negative number gives Error(Nil).
int.square_root(16) // Ok(4.0) square_root = (x : Int) -> Result(Float, Nil) { sum
The sum of the numbers. The sum of no numbers is 0.
sum = (numbers : List(Int)) -> Int { product
The product of the numbers. The product of no numbers is 1.
product = (numbers : List(Int)) -> Int { digits
The digits of a number in a base of 2 or more, highest first. The sign
is not kept. A base less than 2 gives Error(Nil).
int.digits(234, 10) // Ok([2, 3, 4]) digits = (x : Int, base : Int) -> Result(List(Int), Nil) { undigits
The number made from digits in a base of 2 or more, highest first. A
base less than 2, or a digit that is not from 0 to base - 1, gives
Error(Nil).
int.undigits([2, 3, 4], 10) // Ok(234) undigits = (numbers : List(Int), base : Int) -> Result(Int, Nil) { divide
a / by, rounded toward zero, or Error(Nil) when by is 0.
divide = (a : Int, by : Int) -> Result(Int, Nil) { remainder
The remainder of a / by, with the sign of a, or Error(Nil) when
by is 0.
int.remainder(-13, 3) // Ok(-1) remainder = (a : Int, by : Int) -> Result(Int, Nil) { modulo
The remainder of a divided by by, rounded down, with the sign of
by, or Error(Nil) when by is 0.
int.modulo(-13, 3) // Ok(2) modulo = (a : Int, by : Int) -> Result(Int, Nil) { floor_divide
a / by, rounded down, or Error(Nil) when by is 0.
int.floor_divide(-7, 2) // Ok(-4) floor_divide = (a : Int, by : Int) -> Result(Int, Nil) { add
a + b, as a function.
add = (a : Int, b : Int) -> Int { a + b } subtract
a - b, as a function.
subtract = (a : Int, b : Int) -> Int { a - b } multiply
a * b, as a function.
multiply = (a : Int, b : Int) -> Int { a * b } bitwise_and
The bits that are set in both numbers (two’s complement).
bitwise_and = (x : Int, y : Int) -> Int { core.bit_and(x, y) } bitwise_or
The bits that are set in one or both numbers (two’s complement).
bitwise_or = (x : Int, y : Int) -> Int { core.bit_or(x, y) } bitwise_xor
The bits that are set in exactly one of the numbers (two’s complement).
bitwise_xor = (x : Int, y : Int) -> Int { core.bit_xor(x, y) } bitwise_not
Every bit changed: -x - 1.
bitwise_not = (x : Int) -> Int { core.bit_not(x) } bitwise_shift_left
x × 2^y.
bitwise_shift_left = (x : Int, y : Int) -> Int { core.shift_left(x, y) } bitwise_shift_right
x ÷ 2^y, rounded down.
bitwise_shift_right = (x : Int, y : Int) -> Int { core.shift_right(x, y) } range
Folds over the numbers from from to to, with from but without
to. It counts up or down, as needed.
int.range(0, 3, [], (acc, i) { [i, ..acc] }) // [2, 1, 0] range = <Acc, N>(