Arrays\arrayCompilerTyped()

higher-order accumulator returns Closure pure
((mixed → bool), T[]) → (mixed → Closure | T[])
At a glance — Self-returning array builder with a type gate — values that fail the validator are dropped. Useful for safely collecting items of a single expected type.

Like arrayCompiler() but guarded by a validator — values that fail the validator are silently dropped rather than appended.

/**
  * @param callable(mixed):bool $validator Applied before each append.
  * @param mixed[] $inital Initial array contents.
  * @return Closure
  */
Arrays\arrayCompilerTyped(callable $validator, array $inital = []): Closure

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

$compile = Arrays\arrayCompilerTyped('is_int');

$compile = $compile(1);
$compile = $compile('two');  // dropped — not int
$compile = $compile(3);

var_dump($compile()); // [1, 3]

Curried

This can be called inline using currying.

var_dump(Arrays\arrayCompilerTyped('is_string')('a')(1)('b')()); // ['a', 'b']

Details

Arrays Functions

Releated Array compiler Functions