bentools/iterable-functions
Small PHP utility package offering array-like helpers for any iterable (arrays, Traversable, generators): iterable_to_array/to_traversable, map, merge, reduce, filter, values, and chunk—write iterable-agnostic code without TypeError surprises.
iterable()->map()->filter()) mirrors Laravel’s Collection chaining, reducing cognitive load for developers.iterable_chunk() and lazy evaluation (generators) are critical for Laravel’s batch processing (e.g., CSV imports, API pagination). Reduces peak memory usage compared to loading entire arrays into memory.Traversable/iterable type hints in Laravel’s contracts (e.g., Illuminate\Contracts\Pagination\LengthAwarePaginator) make this package a natural fit for input normalization.iterator_to_array()) fail on arrays, improving robustness in data pipelines (e.g., form submissions, query results).iterable_map()) overlaps with Laravel’s Collection::map(), though this package offers broader Traversable support.iterable()->asArray()) can bridge gaps where Collections don’t support generators (e.g., Collection::from($generator) requires conversion).User::cursor()) without loading all records into memory.iterable_reduce() for aggregating job results).iterable_chunk() for CSV imports >10MB").iterable() for generators/iterators, Collection for arrays.array_* functions are faster than this package’s iterable equivalents?iterable_map() vs. array_map() for small/large datasets.app('iterable')->chunk())?iterator_to_array() on arrays need to be updated?iterable_to_array() consistently in test doubles.| Laravel Component | Package Function | Example Use Case |
|---|---|---|
| Eloquent (Query Builder) | iterable_chunk(), iterable_map() |
Process large User::cursor() results lazily. |
| Collections | iterable_to_array(), fluent API |
Convert generator to Collection. |
| API Responses | iterable_merge() |
Merge paginated API responses into a single iterable. |
| Queues/Jobs | iterable_reduce() |
Aggregate results from batched jobs. |
| Form Requests | iterable_filter() |
Validate/clean nested iterable inputs. |
| CSV/Excel Imports | iterable_chunk() |
Stream large files without memory overload. |
| Events | iterable_to_traversable() |
Normalize event payloads to Traversable. |
Phase 1: Pilot Project
iterator_to_array($array) with iterable_to_array($array).iterable_chunk() for batch processing instead of array_chunk().use function BenTools\IterableFunctions\* to the module’s bootstrap file.Phase 2: Core Integration
// app/Providers/IterableServiceProvider.php
public function register()
{
app()->singleton('iterable', function () {
return new class {
public function chunk(iterable $iterable, int $size) {
return iterable_chunk($iterable, $size);
}
// ... other wrappers
};
});
}
iterable_to_array() consistently.Phase 3: Full Adoption
iterator_to_array() on arrays may need updates.composer why-not bentools/iterable-functions to check for conflicts.iterator_to_array($array) usage (search for iterator_to_array).composer.json alias (e.g., "iterable-to-array": "bentools/iterable-functions:iterable_to_array") for gradual replacement.composer update).iterable_chunk() addition).How can I help you explore Laravel packages today?