Example #1
0
 function test_takeWhile_all()
 {
     $takeWhile = takeWhile(function ($value) {
         return $value % 2 > 0;
     });
     $input = [1, 3, 5];
     $expect = $input;
     $actual = iterator_to_array($takeWhile($input));
     $this->assertEquals($expect, $actual);
 }
Example #2
0
 public function testTakeOrDropWhile()
 {
     $this->assertSame([3, 1, 4], toArray(takeWhile(fn\operator('>', 0), [3, 1, 4, -1, 5])));
     $this->assertSame([-1, 5], toArray(dropWhile(fn\operator('>', 0), [3, 1, 4, -1, 5])));
     $this->assertSame([1, 2, 3], toArray(takeWhile(fn\operator('>', 0), [1, 2, 3])));
     $this->assertSame([], toArray(dropWhile(fn\operator('>', 0), [1, 2, 3])));
 }
Example #3
0
/**
 * Removes elements from an array while they match the given predicate. It stops at the
 * first element not matching the predicate and does not remove it.
 * ```php
 * $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'];
 * removeWhile(startsWith('F'), $items) // ['Dev', 'Bar', 'Baz']
 * removeWhile(startsWith('D'), $items) // ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']
 * ```
 *
 * @signature (a -> Boolean) -> [a] -> [a]
 * @param  callable $predicate
 * @param  array $list
 * @return array
 */
function removeWhile()
{
    $removeWhile = function ($predicate, $list) {
        return remove(length(takeWhile($predicate, $list)), $list);
    };
    return apply(curry($removeWhile), func_get_args());
}