∞
Infinity
Create infinite linked lists in JavaScript. Each item is linked to the next and previous item.
This can also be used as an nth generator with a pure interface (see example).
Installation
Node:
npm install --save @codefeathers/infinity
In the browser:
<script src="https://unpkg.com/@codefeathers/infinity">
Usage
const infinity = new InfiniteList(<start:Number>, <next:Function>);
infinity.get(<index:Number>);
infinity[<index:Number>];
infinity.take(<number:Number>);
infinity.take(<startIndex:Number>, <endIndex:Number>);
infinity.get(<index:Number>).next(<number:Number>);
infinity.get(<index:Number>).previous(<number:Number>);
You can pass in any starting value. infinity
cheerfully ignores what you pass in there. The next
function gets current value and (optionally) previous value as arguments to find next value.
InfiniteList
is iterable.
for (let item of infinity) {
console.log(item);
}
Example
const InfiniteList = require('@codefeathers/infinity');
const { log } = console;
const next = (cur, prev) => cur + ((!prev && prev !== 0) ? 1 : prev);
const fibonacci = new InfiniteList(0, next);
log(fibonacci.take(10).map(x => x.value));
log(fibonacci.get(50).value);
log(fibonacci[50].value);
log(fibonacci.get(50).next().value);
log(fibonacci.get(50).next(5).value);
log(fibonacci.last().value);