Version: v0.8.1 - Beta.  We welcome contributors & feedback.

Math.range

Math.range($start, $end, $step=1) -> list

Description

Create a list of numbers from $start to $end (inclusive).

If $end is less than $start, the numbers will appear in descending order.

If $step is given, the numbers will appear in intervals.

Math.range(1, 5)   //= [1, 2, 3, 4, 5]
Math.range(5, 1)   //= [5, 4, 3, 2, 1]
Math.range(-3, 0)  //= [-3, -2, -1, 0]

// With step of 2
Math.range(0, 10, 2)
//= [0, 2, 4, 6, 8, 10]

// With floats
Math.range(0.1, 0.5, 0.1)
//= [0.1, 0.2, 0.3, 0.4, 0.5]

Math.range vs range

There is a subtle difference between Math.range and the bare range function.

// bare `range` function (iterator)
foreach range(1, 1000) as $num {
    ...
}

// Math.range function - List object
$myNums = Math.range(1, 1000)
foreach $myNums as $num {
    ...
}

See Also