generate
优质
小牛编辑
138浏览
2023-12-01
Creates a GeneratedSequence using the specified generator function and (optionally) length.
Signature
Lazy.generate = function(generatorFn, length) { /*...*/ }
Lazy.generate = function generate(generatorFn, length) { return new GeneratedSequence(generatorFn, length); }
Name | Type(s) | Description |
---|---|---|
generatorFn | function(number):* | The function used to generate the sequence. This function accepts an index as a parameter and should return a value for that index in the resulting sequence. |
length | number? | The length of the sequence, for sequences with a definite length. |
returns | GeneratedSequence | The generated sequence. |
Examples
var randomNumbers = Lazy.generate(Math.random); var countingNumbers = Lazy.generate(function(i) { return i + 1; }, 5); randomNumbers // instanceof Lazy.GeneratedSequence randomNumbers.length() // => undefined countingNumbers // sequence: [1, 2, 3, 4, 5] countingNumbers.length() // => 5