Arrays\mapWith()

higher-order transformer variadic lazy accepts iterable returns Closure pure
((T, ...any) → U, ...any) → (Iterable → Iterable)
At a glance — Like map but with extra arguments threaded into every call — useful for mappers that need context beyond the value itself.

Creates a Closure for mapping an array or iterable where the callback receives each element alongside additional bound arguments.

/**
  * @param callable(mixed ...):mixed $func
  * @param mixed ...$data Extra args passed after each value.
  * @return Closure(iterable<int|string, mixed>):(array<int|string, mixed>|\Generator<int|string, mixed>)
  */
Arrays\mapWith(callable $func, ...$data): Closure

Returned Closure

When Arrays\mapWith() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param iterable<int|string, mixed> $source
  * @return array<int|string, mixed>|\Generator<int|string, mixed>
  */
$function (iterable $source): array|\Generator

Examples

Partial Application

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

$join = Arrays\mapWith(fn($v, $sep, $suf) => $v . $sep . $suf, '-', '!');

print_r($join(['foo', 'bar'])); // ['foo-!', 'bar-!']

Curried

This can be called inline using currying.

print_r(Arrays\mapWith('str_repeat', 2)(['ab', 'cd'])); // ['abab', 'cdcd']

Details

  • Group: Arrays
  • Subgroup: Array map
  • Since: 0.1.0
  • Source: