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

first

优质
小牛编辑
134浏览
2023-12-01

Creates a new sequence comprising the first N elements from this sequence, OR (if N is undefined) simply returns the first element of this sequence.

Signature

Sequence.first = function(count) { /*...*/ }
Sequence.first = function first(count) {
  if (typeof count === "undefined") {
return getFirst(this);
  }
  return new TakeSequence(this, count);
}
NameType(s)Description
countnumber?

The number of elements to take from this sequence. If this value exceeds the length of the sequence, the resulting sequence will be essentially the same as this one.

returns*

The new sequence (or the first element from this sequence if no count was given).

Examples

function powerOfTwo(exp) {
  return Math.pow(2, exp);
}

Lazy.generate(powerOfTwo).first()          // => 1
Lazy.generate(powerOfTwo).first(5)         // sequence: [1, 2, 4, 8, 16]
Lazy.generate(powerOfTwo).skip(2).first()  // => 4
Lazy.generate(powerOfTwo).skip(2).first(2) // sequence: [4, 8]