takeWhile
优质
小牛编辑
144浏览
2023-12-01
Creates a new sequence comprising the elements from the head of this sequence that satisfy some predicate. Once an element is encountered that doesn't satisfy the predicate, iteration will stop.
Signature
Sequence.takeWhile = function(predicate) { /*...*/ }
Sequence.takeWhile = function takeWhile(predicate) { return new TakeWhileSequence(this, predicate); }
Name | Type(s) | Description |
---|---|---|
predicate | Function | |
returns | Sequence | The new sequence |
Examples
function lessThan(x) { return function(y) { return y < x; }; } Lazy([1, 2, 3, 4]).takeWhile(lessThan(3)) // sequence: [1, 2] Lazy([1, 2, 3, 4]).takeWhile(lessThan(0)) // sequence: []