lazyarray-lite
A small lazy array class implementation.
Instead of generating a potentially huge array of objects (permutations, subsets, or whatever) only the required items are generated. Items that are not used by the program will never be generated and do not waste time and space in them
Version
0.2.1
Installation
$ npm install lazyarray-lite
Example
var LazyArray =var la = {return 2 * i} // 0, 2, 4, 6, 8...la // [2, 6, 8]la // 10
API
constructor (options)
Create a new lazy array data type.
options
-
Type: Object
Options to pass to lazy array constructor
-
Type: Function
Syntactic sugar for
options.get
.Example:
var la = {return position * position} // 0, 1, 4, 9, 16, 25...For more information see
option.get
.
options.get(position)
Type: Function
Callback that define the formula to get element of lazy array based on its position.
Example:
var la ={return position * position}// 0, 1, 4, 9, 16, 25...
It returns element of lazy array or undefined. If options.get(position)
returns undefined, it is assumed that there are not more elements and lazy array is finite and maximum length is position
.
options.next(predecessors...)
Type: Function
When is not possible to define element of lazy array based on its position, options.next
define the formula to get the value based on its predecessors.
options.next
must be used with some integer index option that defines the first values of the lazy array.
Example:
// odd listla =0: 1{return n + 2}// 1, 3, 5, 7, 9, 11...
or
// Fibonacci listla =0: 01: 1{return a + b}// 0, 1, 1, 2, 3, 5, 8, 13...
It returns element of lazy array or undefined. If options.next
returns undefined it is assumed that there are not more elements and lazy array is finite.
options.{integer index}
Type: !== undefined
Integer indexs (0, 1, 2, 10, 234, etc) correspond to elements of lazy array that are computed before to get for first time.
options.init
Type: Function
There are sequences that can not be expressed with a formula or based on its predecessors. It possible to use extra values in options.next
with this
context and initialize this values in options.init
.
Example:
// triangular sequencela =0: 0{thisvalue = 0}{thisvalue++return n + thisvalue}// 0, 1, 3, 6, 10, 15, 21...
or
// Fibonacci listla ={thisnextToCheck = 2}{var toCheck = thisnextToCheckwhile !toCheck++thisnextToCheck = toCheck + 1return toCheck}// 2, 3, 5, 7, 11, 13, 17, 19...{var top = Mathfor i = 2; i <= top; ++iif num % i === 0breakreturn i > top}
options.maxLength
Type: Integer?
It defines the maximum length of lazy array. If options.maxLength
is undefined
or not defined, the lazy array created is infinity in potencial.
Example:
var la ={return position * position}maxLength: 5// 0, 1, 4, 9, 16. (STOP)la // 4la // undefined
Advice:
It is advisable to use options.get
on constructor whenever possible because it has a higher performance. Using options.next
property function, to get an array element before is necessary to compute all its predecessors.
.get (index)
returns the specified element from a lazy array.
index
Type: Integer
zero-based index to get element of lazy array.
Caution: This method uses memoization. Modify returned element of lazy array can produce unexpected behaviours.
.slice(begin, end)
returns a portion of a lazy array into a new array object.
begin
Type: Integer
zero-based index at which to begin extraction
end
Type: Integer
zero-based index at which to end extraction.
Caution: This method uses memoization. Modify returned elements of lazy array can produce unexpected behaviours.
.maxLength
Type: Integer | +Infinity
gets max length of lazy array.
Development
Want to contribute? Great!
https://github.com/xgbuils/lazyarray-lite
Todo's
...
License
MIT