range.lua
View source code here on GitHub!
Note
This module directly ports some of the logic found in Python's range
. Because of that,
these functions are 0-indexed.
- range_entry3(start, step, idx)
- Returns:
start + (step * idx)
- Return type:
number
- range_entry4(start, stop, step, idx)
- Returns:
The
idx
th entry ofstart,(stop-1),step
- Return type:
number
1-- This file is a direct port of Python's range functions
2
3local function range_entry3(start, step, idx)
4 return start + (step * idx)
5end
6
7local function range_entry4(start, stop, step, idx)
8 local length = 0
9 if step > 0 and start < stop then
10 length = math.floor(1 + (stop - 1 - start) / step)
11 elseif step < 0 and start > stop then
12 length = math.floor(1 + (start - 1 - stop) / (-step))
13 end
14 if idx < 0 then
15 idx = length + idx
16 end
17 if idx < 0 or idx >= length then
18 return nil, "length is 0, cannot fetch"
19 end
20 return range_entry3(start, step, idx)
21end
22
23return {
24 range_entry3 = range_entry3,
25 range_entry4 = range_entry4,
26}