@acdibble/lazy-range 中文文档教程
Python 3 range()
in JS
这是一个内存高效的范围类。 它只存储开始,停止,步骤, 和一个范围的长度。 这些值是根据需要生成的 Symbol.iterator
。 它适用于任何兼容 ES2015 的浏览器。
此外,它已经过调整以匹配预期的 JavaScript 行为,例如 使用 LazyRange#at
查找越界索引将返回 undefined,而不是 而不是抛出错误。
Examples:
const LazyRange = require('@acdibble/lazy-range');
或模块:
import LazyRange from '@acdibble/lazy-range';
Just a stop parameter:
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> [...new LazyRange(10)];
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
> Array.from(new LazyRange(10));
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
With start and stop parameter:
>>> list(range(-10, 0))
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
> [...new LazyRange(-10, 0)];
[ -10, -9, -8, -7, -6, -5, -4, -3, -2, -1 ]
With start, stop, and step parameters:
>>> list(range(1, 100, 25))
[1, 26, 51, 76]
> [...new LazyRange(1, 100, 25)];
[ 1, 26, 51, 76 ]
Iteration:
>>> sum = 0
>>> for x in range(10):
... sum += x
...
>>> sum
45
> let sum = 0;
undefined
> for (const x of new LazyRange(10)) {
... sum += x;
... }
45
或
> [...new LazyRange(10)].reduce((acc, num) => acc + num, 0);
45
Equality:
range(0, 3, 2) == range(0, 4, 2)
True
> new LazyRange(0, 3, 2).equals(new LazyRange(0, 4, 2))
true
Slice:
>>> range(0, 20, 2)[4:-3:2]
range(8, 14, 4)
> new LazyRange(0, 20, 2).slice(4, -3, 2);
LazyRange { start: 8, step: 4, stop: 14, length: 2 }
Length:
>>> len(range(10))
10
> new LazyRange(10).length;
10
Presence:
>>> 10 in range(0, 20, 2)
True
> new LazyRange(0, 20, 2).has(10);
true
Lookup by index:
>>> range(1, 19, 3)[5]
16
> new LazyRange(1, 19, 3).at(5);
16
Find index:
>>> range(1, 19, 3).index(13)
4
> new LazyRange(1, 19, 3).indexOf(13);
4
Python 3 range()
in JS
This is a memory-efficient range class. It only stores the start, stop, step, and length for a range. The values are generated as necessary via Symbol.iterator
. It works with any ES2015 compliant browsers.
Additionally, it's been adapted to match expected JavaScript behavior, e.g. using LazyRange#at
to look up an out of bounds index will return undefined, rather than throwing an error.
Examples:
const LazyRange = require('@acdibble/lazy-range');
or with modules:
import LazyRange from '@acdibble/lazy-range';
Just a stop parameter:
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> [...new LazyRange(10)];
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
> Array.from(new LazyRange(10));
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
With start and stop parameter:
>>> list(range(-10, 0))
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
> [...new LazyRange(-10, 0)];
[ -10, -9, -8, -7, -6, -5, -4, -3, -2, -1 ]
With start, stop, and step parameters:
>>> list(range(1, 100, 25))
[1, 26, 51, 76]
> [...new LazyRange(1, 100, 25)];
[ 1, 26, 51, 76 ]
Iteration:
>>> sum = 0
>>> for x in range(10):
... sum += x
...
>>> sum
45
> let sum = 0;
undefined
> for (const x of new LazyRange(10)) {
... sum += x;
... }
45
or
> [...new LazyRange(10)].reduce((acc, num) => acc + num, 0);
45
Equality:
range(0, 3, 2) == range(0, 4, 2)
True
> new LazyRange(0, 3, 2).equals(new LazyRange(0, 4, 2))
true
Slice:
>>> range(0, 20, 2)[4:-3:2]
range(8, 14, 4)
> new LazyRange(0, 20, 2).slice(4, -3, 2);
LazyRange { start: 8, step: 4, stop: 14, length: 2 }
Length:
>>> len(range(10))
10
> new LazyRange(10).length;
10
Presence:
>>> 10 in range(0, 20, 2)
True
> new LazyRange(0, 20, 2).has(10);
true
Lookup by index:
>>> range(1, 19, 3)[5]
16
> new LazyRange(1, 19, 3).at(5);
16
Find index:
>>> range(1, 19, 3).index(13)
4
> new LazyRange(1, 19, 3).indexOf(13);
4