luna-utils

A utils library, make operations more efficient and guarded by luna-generic.

utils
math
moon add Luna-Flow/luna-utils@0.1.1
Download zip
Author
Version
0.1.1
License
MIT
Last updated
9 months ago
Downloads
2K

Dependencies

README

#luna-utils

#
arr_max

fn[T : Compare + Eq] arr_max(array : Array[T]) -> T

Finds the maximum element in an array.

Parameters:

  • array : Array[T] - The array to search for the maximum element.

Returns the maximum element of type T found in the array.

Panics if the array is empty (when accessing array[0]).

Example:

let numbers = [1, 5, 3, 9, 2]
let max_num = arr_max(numbers)
inspect(max_num, content="9")

#
arr_min

fn[T : Compare + Eq] arr_min(array : Array[T]) -> T

Finds the minimum element in an array by comparing all elements.

Parameters:

  • array : The array to find the minimum element from.

Returns the minimum element found in the array.

Panics if the array is empty (when accessing array[0]).

Example:

let numbers = [5, 2, 8, 1, 9]
inspect(arr_min(numbers), content="1")

#
arr_sum

Computes the sum of all elements in an array using the additive monoid operation.

Parameters:

  • arr : The array of elements to sum.

Returns the sum of all elements in the array, or the zero element of the monoid if the array is empty.

Example:

let numbers = [1, 2, 3, 4, 5]
inspect(arr_sum(numbers), content="15")

let empty_array : Array[Int] = []
inspect(arr_sum(empty_array), content="0")

#
clamp

fn[T : Compare + Eq] clamp(value : T, min : T, max : T) -> T

Constrains a value to lie within a specified range by returning the closest value within the bounds.

Parameters:

  • value : The value to be clamped.
  • min : The minimum bound of the range.
  • max : The maximum bound of the range.

Returns the clamped value: min if value is less than min, max if value is greater than max, otherwise value itself.

Example:

inspect(clamp(5, 1, 10), content="5")
inspect(clamp(-3, 1, 10), content="1")
inspect(clamp(15, 1, 10), content="10")

#
find

fn[T : Eq] find(array : Array[T], value : T) -> Int?

Searches for the first occurrence of a value in an array and returns its index.

Parameters:

  • array : The array to search in.
  • value : The value to search for.

Returns Some(index) if the value is found, where index is the position of the first occurrence, or None if the value is not found in the array.

Example:

let arr = [1, 2, 3, 2, 4]
inspect(find(arr, 2), content="Some(1)")
inspect(find(arr, 5), content="None")

#
is_between

fn[T : Compare + Eq] is_between(value : T, min : T, max : T) -> Bool

Checks if a value falls within the inclusive range defined by minimum and maximum bounds.

Parameters:

  • value : The value to check.
  • min : The minimum bound of the range (inclusive).
  • max : The maximum bound of the range (inclusive).

Returns true if the value is within the range [min, max], false otherwise.

Example:

inspect(is_between(5, 1, 10), content="true")
inspect(is_between(0, 1, 10), content="false")
inspect(is_between(10, 1, 10), content="true")

#
map_same

fn[U, V : Eq] map_same(array : Array[U], f : (U) -> V) -> Bool

let same_parity = [2, 4, 6, 8] inspect(map_same(same_parity, fn(x) { x % 2 }), content="true")

#
map_same_to

fn[U, V : Eq] map_same_to(array : Array[U], sample : V, f : (U) -> V) -> Bool

Checks if all elements in an array, when mapped by a function, are equal to a given sample value.

Parameters:

  • array : The input array of elements to be transformed and checked.
  • sample : The expected value that all mapped elements should equal.
  • f : The transformation function to apply to each element in the array.

Returns true if all elements in the array, when transformed by the function, equal the sample value; false otherwise.

Example:

let numbers = [1, 2, 3, 4]
let double = fn(x) { x * 2 }
inspect(map_same_to(numbers, 6, double), content="false")

let ones = [1, 1, 1]
inspect(map_same_to(ones, 2, double), content="true")

#
reverse

fn[T] reverse(array : Array[T]) -> Array[T]

Creates a new array with elements in reverse order of the input array.

Parameters:

  • array : The array to reverse.

Returns a new array containing the same elements as the input array, but in reverse order.

Example:

let original = [1, 2, 3, 4, 5]
let reversed = reverse(original)
inspect(reversed, content="[5, 4, 3, 2, 1]")

#
reverse_inplace

fn[T] reverse_inplace(arr : Array[T]) -> Unit

Reverses the elements of an array in place by swapping elements from both ends toward the center.

Parameters:

  • arr : The array to be reversed in place.

Panics if the array is empty when accessing arr[0] to determine the array's length.

Example:

let numbers = [1, 2, 3, 4, 5]
reverse_inplace(numbers)
inspect(numbers, content="[5, 4, 3, 2, 1]")

#
same

fn[T : Eq] same(array : Array[T]) -> Bool

Checks if all elements in an array are equal to each other.

Parameters:

  • array : The array to check for uniformity.

Returns true if all elements in the array are equal to the first element, false otherwise.

Panics if the array is empty (when accessing array[0]).

Example:

let uniform = [1, 1, 1, 1]
let mixed = [1, 2, 1, 1]
inspect(same(uniform), content="true")
inspect(same(mixed), content="false")

#
same_to

fn[T : Eq] same_to(array : Array[T], sample : T) -> Bool

Checks if all elements in an array are equal to a specific value.

Parameters:

  • array : The array to check.
  • sample : The value to compare all elements against.

Returns true if all elements in the array are equal to the sample value, false otherwise.

Example:

let arr1 = [1, 1, 1, 1]
let arr2 = [1, 2, 1, 1]
inspect(same_to(arr1, 1), content="true")
inspect(same_to(arr2, 1), content="false")

#
zero_arr

Creates an array of length n filled with zero values of type A.

Parameters:

  • n : The length of the array to create.

Returns an array of length n where each element is the zero value of type A.

Example:

let zeros:Array[Int] = zero_arr(5) // Creates [0, 0, 0, 0, 0] for Int
inspect(zeros.length(), content="5")
inspect(zeros[0], content="0")

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io