Skip to content

IEnumerable.takeWhile() method

Marcel Kloubert edited this page Jun 26, 2021 · 5 revisions

IEnumerable->takeWhile($predicate) method

Takes elements in that sequence as long as a specified condition is true (s. TakeWhile()).

Syntax

public function takeWhile(predicate $predicate) : IEnumerable;
Name Type Description
$predicate predicate The condition to use.

$predicate

The condition to use.

Result

The new sequence.

Examples

Lambda expression

use \System\Linq\Enumerable;

$seq = Enumerable::create([239, 5979, 22, 1]);

foreach ($seq->takeWhile('$x => $x != 22') as $item) {
    // [0] 239
    // [1] 5979
}

Closure

use \System\Linq\Enumerable;

$seq = Enumerable::create([239, 5979, 22, 1]);

foreach ($seq->takeWhile(function ($x) {
                             return $x != 22;
                         }) as $item) {
    // [0] 239
    // [1] 5979
}
Clone this wiki locally