当前位置: 首页 > 文档资料 > Lazy.js 英文文档 >

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);
}
NameType(s)Description
generatorFnfunction(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.

lengthnumber?

The length of the sequence, for sequences with a definite length.

returnsGeneratedSequence

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