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()->filter()->map()) simplifies complex data transformations, reducing cognitive load for developers working with collections, streams, or Eloquent results.iterable_chunk() or iterable_map() to avoid memory spikes.iterable_filter()).iterable_reduce() for log analysis).Adopt if:
Traversable objects) and encounters quirks like iterator_to_array() failing on arrays.map, filter, reduce) but needs consistency across iterables.Avoid if:
array_map()) despite edge cases, or already uses Laravel’s Collection class extensively.league/iterators.Look elsewhere if:
reactphp or Laravel’s ShouldQueue contracts.foreach)."This package solves a hidden technical debt problem: Laravel apps often mix arrays, generators, and iterators, leading to fragile code (e.g., iterator_to_array() breaking on arrays). By adopting bentools/iterable-functions, we standardize data processing across the codebase—reducing bugs, cutting memory usage for large datasets (e.g., imports, APIs), and speeding up development with cleaner, chainable operations. It’s a 10-minute install with measurable benefits: fewer production issues, lower server costs for batch jobs, and happier devs writing less boilerplate. Think of it as ‘Laravel Collections for iterables’—but lighter and more flexible."
Key Ask: "Approve a 2-week pilot to integrate this into our data pipelines (e.g., API responses, CSV imports). We’ll measure memory savings and developer productivity gains."
*"This package gives us superpowers for iterables without the pain:
TypeErrors: iterable_to_array() works on any iterable (arrays, generators, Traversable).iterable_chunk() or iterable_map() to handle millions of records without OOM crashes.foreach loops with readable chains like iterable($data)->filter()->map()->asArray().When to use it: ✅ Processing paginated APIs, large exports, or event streams. ✅ Migrating legacy array logic to iterables/generators. ✅ Writing tests with mock iterables.
When to skip it: ❌ Your code is 100% arrays (use native functions instead). ❌ You’re on PHP < 7.1 (drop support in v2.0).
Next Steps:
array_* functions.iterable_map()/iterable_filter() to test readability gains.*"Tired of writing custom iterable helpers or debugging iterator_to_array() on arrays? This package gives you array-like functions for any iterable—arrays, generators, Traversable objects—with zero edge cases.
Examples:
// Before: Fragile
$array = iterator_to_array(new ArrayIterator([1, 2])); // ❌ TypeError!
$array = iterator_to_array([1, 2]); // ❌ TypeError!
// After: Robust
use BenTools\IterableFunctions\iterable_to_array;
$array = iterable_to_array([1, 2]); // ✅ [1, 2]
$array = iterable_to_array(new ArrayIterator([1, 2])); // ✅ [1, 2]
// Fluent chains (like Collections but for iterables):
$cleaned = iterable($data)
->filter(fn($item) => $item->isValid())
->map(fn($item) => $item->serialize())
->asArray();
Why it’s awesome:
Traversable.Try it today:
composer require bentools/iterable-functions
Then replace your next foreach/array_map loop with iterable_map() and thank me later."*
How can I help you explore Laravel packages today?