Arrays that look just like regular JavaScript arrays, but are computed lazily. Like Scala or Haskell's lazy streams. Read more about it in the introductory blog post: https://performancejs.com/post/ewffd34/Introducing:-Lazy-arrays-in-JavaScript.
Install
npm i lazy-arr -S
Usage
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
Examples
// even numberslet numbers = numbers0 // 0numbers5 // 10 // fibonacci numbers (with initial value of [0, 1])let fibs = fibs_ - 1 + fibs_ - 2 fibs0 // 0fibs1 // 1fibs10 // 55
Other operations
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
License
MIT