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.
range: returns an Iterator object, which is less flexible, but can conserve memory over very large ranges. This is recommended if you are only looping over a static range of numbers.Math.rangereturns a List object, which takes more memory, but is easier to manipulate.
// bare `range` function (iterator)
foreach range(1, 1000) as $num {
...
}
// Math.range function - List object
$myNums = Math.range(1, 1000)
foreach $myNums as $num {
...
}