ClosureIteratorIterator

Class

An extended IteratorIterator that applies a closure transformation to each element during iteration.

Description

This class allows applying a transformation function dynamically while iterating over an existing Traversable.

Usage Example:

Tags
example

Description

use FastForward\Iterator\ClosureIteratorIterator;
use ArrayIterator;

$array = [1, 2, 3, 4, 5];

$iterator = new ClosureIteratorIterator(
    new ArrayIterator($array),
    fn ($value) => $value * 2
);

foreach ($iterator as $value) {
    echo $value; // Outputs: 2, 4, 6, 8, 10
}
since
1.0.0

Table of Contents

Properties

 : Closure

Methods

__construct()

Initializes the ClosureIteratorIterator.

 : mixed
count()

Counts the number of elements exposed by the inner iterator.

 : int
current()

Returns the current transformed element.

 : mixed
Properties
Methods

__construct()

Public

Initializes the ClosureIteratorIterator.

public __construct( iterable<string|int, mixed>  $iterator, Closure  $closure) : mixed
Parameters
$iterator : iterable<string|int, mixed>

Description

the underlying iterator to wrap

$closure : Closure

Description

the transformation function applied to each element

count()

Public

Counts the number of elements exposed by the inner iterator.

public count() : int

Description

If the inner iterator implements Countable, this method SHALL return the value provided by that implementation. Otherwise, it MUST count elements by iterating over the iterator. If the inner iterator is not cloneable, this method SHALL wrap the current object in an IteratorIterator instance and count through that wrapper to avoid performing an invalid clone operation. If the inner iterator is cloneable, this method SHOULD count over a clone so that the original iterator state is preserved as much as possible.

Return values
int

Description

the total number of elements available from the inner iterator

current()

Public

Returns the current transformed element.

public current() : mixed

Description

The closure is applied to the original current element of the iterator.

Return values

Description

the transformed element