TakeWhile

TakeWhile ( callable $callback )

Returns input values while the callback function returns true, then all remaining values are ignored. The last value returned is the the one previous to the value for which the callback function returned false.

Parameters

callback
bool callback ( mixed $value )
value
The current value.

Examples

Example #1

Basic usage example.

<?php
use Webbhuset\Pipeline\Constructor as F;

$takeWhile = F::TakeWhile(function ($value) {
    return $value < 7;
});

$input = [1, 3, 5, 7, 9, 2, 4, 6, 8];

echo json_encode(iterator_to_array($takeWhile($input)));

// Output: [1,3,5]

See Also

  • Take - Take a specific amount of input values.
  • DropWhile - Discard input values while a callback function returns true.