Int
The Int module provides functions for working with 64-bit signed integers. These are Kit's primary numeric type for whole numbers.
Operators vs Functions
Standard arithmetic operators (+, -, *, /, %)
work directly on integers. The module functions like Int.add are useful for
partial application and pipelines.
Arithmetic
Int.add
Int -> Int -> Int
Adds two integers. Equivalent to the
+ operator.
println (Int.add 3 5)
# => 8
# With pipe operator
3 |> Int.add 5 |> println
# => 8
Int.sub
Int -> Int -> Int
Subtracts the second integer from the first.
println (Int.sub 10 3)
# => 7
Int.mul
Int -> Int -> Int
Multiplies two integers.
println (Int.mul 4 7)
# => 28
Int.div
Int -> Int -> Int
Integer division, truncating toward zero.
println (Int.div 17 5)
# => 3
println (Int.div -17 5)
# => -3
Int.mod
Int -> Int -> Int
Returns the remainder after division.
println (Int.mod 17 5)
# => 2
println (17 % 5)
# => 2
Int.neg
Int -> Int
Negates an integer.
println (Int.neg 5)
# => -5
println (Int.neg -3)
# => 3
Int.abs
Int -> Int
Returns the absolute value.
println (Int.abs -7)
# => 7
println (Int.abs 7)
# => 7
Comparison
Int.eq?
Int -> Int -> Bool
Returns
true if both integers are equal.
println (Int.eq? 5 5)
# => true
println (5 == 5)
# => true
Int.ne?
Int -> Int -> Bool
Returns
true if the integers are not equal.
println (Int.ne? 5 3)
# => true
Int.lt? / Int.gt? / Int.le? / Int.ge?
Int -> Int -> Bool
Less than, greater than, less than or equal, greater than or equal comparisons.
println (Int.lt? 3 5) # => true
println (Int.gt? 5 3) # => true
println (Int.le? 3 3) # => true
println (Int.ge? 5 5) # => true
Int.compare
Int -> Int -> Int
Returns
-1 if first is less, 0 if equal, 1 if greater.
println (Int.compare 3 5) # => -1
println (Int.compare 5 5) # => 0
println (Int.compare 7 5) # => 1
Predicates
Int.even?
Int -> Bool
Returns
true if the integer is even.
println (Int.even? 4) # => true
println (Int.even? 5) # => false
Int.odd?
Int -> Bool
Returns
true if the integer is odd.
println (Int.odd? 5) # => true
println (Int.odd? 4) # => false
Int.positive?
Int -> Bool
Returns
true if the integer is greater than zero.
println (Int.positive? 5) # => true
println (Int.positive? 0) # => false
println (Int.positive? -3) # => false
Int.negative?
Int -> Bool
Returns
true if the integer is less than zero.
println (Int.negative? -5) # => true
println (Int.negative? 0) # => false
Int.zero?
Int -> Bool
Returns
true if the integer is zero.
println (Int.zero? 0) # => true
println (Int.zero? 5) # => false
Conversions
Int.to-float
Int -> Float
Converts an integer to a floating-point number.
x = Int.to-float 42
println x
# => 42.0
Int.to-string / Int.to-str
Int -> String
Converts an integer to its string representation.
Int.to-str is an alias.
s = Int.to-string 42
println s
# => "42"
Int.parse
String -> Result Int ParseError
Parses a string as an integer. Returns
Ok n on success or Err ParseError on invalid input.
result = Int.parse "42"
println result
# => Ok(42)
result = Int.parse "abc"
println result
# => Err(ParseError)
Int.force
Int -> Int
Forces evaluation of an integer value. Useful for strict evaluation in lazy contexts.
n = Int.force 42
println n
# => 42
Int.show
Int -> String
Returns a string representation of the integer (same as
to-string).
println (Int.show -123)
# => "-123"
Bitwise Operations
Int.bit-and
Int -> Int -> Int
Bitwise AND of two integers.
println (Int.bit-and 0b1010 0b1100)
# => 8 (0b1000)
Int.bit-or
Int -> Int -> Int
Bitwise OR of two integers.
println (Int.bit-or 0b1010 0b1100)
# => 14 (0b1110)
Int.bit-xor
Int -> Int -> Int
Bitwise XOR of two integers.
println (Int.bit-xor 0b1010 0b1100)
# => 6 (0b0110)
Int.bit-not
Int -> Int
Bitwise NOT (complement) of an integer.
println (Int.bit-not 0)
# => -1
Int.shift-left
Int -> Int -> Int
Shifts bits left by the given amount.
println (Int.shift-left 1 4)
# => 16
Int.shift-right
Int -> Int -> Int
Shifts bits right by the given amount (arithmetic shift).
println (Int.shift-right 16 2)
# => 4
Min/Max/Clamp
Int.min
Int -> Int -> Int
Returns the smaller of two integers.
println (Int.min 3 7)
# => 3
Int.max
Int -> Int -> Int
Returns the larger of two integers.
println (Int.max 3 7)
# => 7
Int.clamp
Int -> Int -> Int -> Int
Restricts a value to be within a given range.
println (Int.clamp 5 0 10) # => 5 (within range)
println (Int.clamp -5 0 10) # => 0 (clamped to min)
println (Int.clamp 15 0 10) # => 10 (clamped to max)