This pull request introduces two new methods, firstIndex and lastIndex, to the LazyCollection class in Laravel, allowing developers to retrieve the index of the first or last item in a collection that matches a specified condition.
Overview of Changes
firstIndex(callable $callback): TKey|null
Finds and returns the index of the first item in the LazyCollection that satisfies the given condition. Returns null if no items match.
lastIndex(callable $callback): TKey|null
Identifies and returns the index of the last item in the LazyCollection that meets the specified condition. Returns null if no match is found.
Usability & Benefits
These new methods enhance LazyCollection by providing efficient, index-based searching without requiring full materialization of the collection in memory. This addition is especially useful for working with large datasets or when utilizing lazy loading, as it supports:
Efficient, condition-based indexing for large collections
Improved code readability and expressiveness by removing the need for custom iteration logic
Memory optimization, particularly in scenarios where collections are dynamically generated or streamed
Example Usage:
$collection = LazyCollection::make([10, 20, 30, 40, 50]);
// Get the index of the first item greater than 25
$firstIndex = $collection->firstIndex(fn($value) => $value > 25); // Returns 2
// Get the index of the last item greater than 25
$lastIndex = $collection->lastIndex(fn($value) => $value > 25); // Returns 4
$collection = new Collection([10, 20, 30, 40, 50]);
// Get the index of the first item greater than 25
$firstIndex = $collection->firstIndex(fn($value) => $value > 25); // Returns 2
// Get the index of the last item greater than 25
$lastIndex = $collection->lastIndex(fn($value) => $value > 25); // Returns 4
These methods are consistent with Laravel's collection functionality, promoting efficient lazy evaluation and optimized handling of large data sources.
This pull request introduces two new methods, firstIndex and lastIndex, to the LazyCollection class in Laravel, allowing developers to retrieve the index of the first or last item in a collection that matches a specified condition.
Overview of Changes firstIndex(callable $callback): TKey|null Finds and returns the index of the first item in the LazyCollection that satisfies the given condition. Returns null if no items match.
lastIndex(callable $callback): TKey|null Identifies and returns the index of the last item in the LazyCollection that meets the specified condition. Returns null if no match is found.
Usability & Benefits These new methods enhance LazyCollection by providing efficient, index-based searching without requiring full materialization of the collection in memory. This addition is especially useful for working with large datasets or when utilizing lazy loading, as it supports:
Efficient, condition-based indexing for large collections Improved code readability and expressiveness by removing the need for custom iteration logic Memory optimization, particularly in scenarios where collections are dynamically generated or streamed
Example Usage:
These methods are consistent with Laravel's collection functionality, promoting efficient lazy evaluation and optimized handling of large data sources.