March Docs › Range Range Range module: integer ranges as first-class values.
A range is a record %{start: Int, stop: Int, step: Int}. Ranges are half-open: [start, stop) when step > 0, (stop, start] when step < 0. step must be non-zero; new/1 defaults to step = 1.
Functions fn new new(start : Int, stop : Int) : %{start: Int, stop: Int, step: Int} # Creates a range [start, stop) with step 1.
fn new_step new_step(start : Int, stop : Int, step : Int) : %{start: Int, stop: Int, step: Int} # Creates a range with an explicit step. Step must be non-zero.
fn to_list to_list(r : %{start: Int, stop: Int, step: Int}) : List(Int) # Converts the range to a list of integers.
fn contains contains(r : %{start: Int, stop: Int, step: Int}, n : Int) : Bool # Returns true if n is within the range.
fn size size(r : %{start: Int, stop: Int, step: Int}) : Int # Returns the number of elements in the range.
fn is_empty is_empty(r : %{start: Int, stop: Int, step: Int}) : Bool # Returns true if the range contains no elements.
fn map map(r : %{start: Int, stop: Int, step: Int}, f : Int -> b) : List(b) # Applies f to each element, returning a list of results.
fn filter filter(r : %{start: Int, stop: Int, step: Int}, f : Int -> Bool) : List(Int) # Returns a list of elements satisfying predicate f.
fn each each(r : %{start: Int, stop: Int, step: Int}, f : Int -> Unit) : Unit # Calls f for each element in the range (for side effects).
fn reduce reduce(r : %{start: Int, stop: Int, step: Int}, acc : b, f : (b, Int) -> b) : b # Folds over the range: f(acc, element) -> acc.
fn reverse reverse(r : %{start: Int, stop: Int, step: Int}) : %{start: Int, stop: Int, step: Int} # Returns a new range iterating in the opposite direction.
fn first first(r : %{start: Int, stop: Int, step: Int}) : Option(Int) # Returns Some of the first element, or None if empty.
fn last last(r : %{start: Int, stop: Int, step: Int}) : Option(Int) # Returns Some of the last element, or None if empty.
fn all all(r : %{start: Int, stop: Int, step: Int}, f : Int -> Bool) : Bool # Returns true if all elements satisfy f.
fn any any(r : %{start: Int, stop: Int, step: Int}, f : Int -> Bool) : Bool # Returns true if any element satisfies f.
fn sum sum(r : %{start: Int, stop: Int, step: Int}) : Int # Returns the sum of all elements in the range.