npm i lazy-arr -S
Lazy-arr takes a function, and uses it to lazily generate values for the array. The function takes a numerical array index (eg.
5) and should return the value for that index in the array. The function doesn't have to be idempotent, but its return value will be cached (you can then delete it from cache, if you want).
It supports 2 usage patterns:
- Call it with just a function:
- Call it with a function and an initial value:
let seq = index + seqindex - 1
// even numberslet numbers =numbers0 // 0numbers5 // 10// fibonacci numbers (with initial value of [0, 1])let fibs = fibs_ - 1 + fibs_ - 2fibs0 // 0fibs1 // 1fibs10 // 55
let numbers =numbers3 // 6// membership2 in numbers // true3 in numbers // true4 in numbers // false// deletingdelete numbers33 in numbers // false
Note that you cannot directly set values:
let numbers =numbers7 = 3 // THROWS ERROR