sanmai / lazy-linq
.NET LINQ-like library with mandatory lazy evaluation
Requires
- php: ^7.0
- sanmai/pipeline: ^2 || ^3 || ^4
Requires (Dev)
- codacy/coverage: ^1.4
- friendsofphp/php-cs-fixer: ^2.11
- infection/infection: >=0.8
- mockery/mockery: ^1.0
- phan/phan: >=0.11
- php-coveralls/php-coveralls: ^2.0
- phpstan/phpstan: >=0.9
- phpunit/phpunit: >=6
- vimeo/psalm: >=1.1
This package is auto-updated.
Last update: 2021-01-02 15:20:30 UTC
README
This library implements most of the API methods from .NET static class System.Linq.Enumerable
which do not require having the whole data set in memory. If you're used to LINQ query operations from .NET, you should feel like home.
Powered by generators, this library works in a lazy way: instead of doing computing on the whole sequence at once, it would only do necessary computing while you're iterating through the sequence.
Install
composer require sanmai/lazy-linq
API Compatibility
Method | Details | |
---|---|---|
✔️ | Aggregate() | |
✔️ | All() | |
✔️ | Any() | |
✔️ | Append() | Returns a new collection. |
AsEnumerable() | Not applicable to PHP. | |
✔️ | Average() | |
✔️ | Cast() | |
✔️ | Concat() | |
✔️ | Contains() | |
✔️ | Count() | |
DefaultIfEmpty() | Not applicable. | |
✔️ | Distinct() | Only removes repeated elements, think uniq . |
✔️ | ElementAt() | @throws 💣 |
✔️ | ElementAtOrDefault() | Always a null default value, @throws 💣 |
✔️ | Empty() | Returns a new empty collection. |
✔️ | Except() | Returns a new collection. |
✔️ | First() | |
FirstOrDefault() | Collections are not typed in PHP. | |
GroupBy() | Needs all data at once. | |
GroupJoin() | ||
Intersect() | ||
Join() | ||
✔️ | Last() | |
LastOrDefault() | Not applicable. | |
LongCount() | No separate long type in PHP. |
|
✔️ | Max() | |
✔️ | Min() | |
✔️ | OfType() | There's also ofClass() for classes. |
OrderBy() | Needs to have the whole set of data in memory to function. | |
OrderByDescending() | ||
✔️ | Prepend() | Returns a new collection. |
✔️ | Range() | Semantically different from standard range() function. |
✔️ | Repeat() | |
Reverse() | Needs all data at once. | |
✔️ | Select() | |
✔️ | SelectMany() | |
SequenceEqual() | ||
✔️ | Single() | @throws 💣 |
SingleOrDefault() | ||
✔️ | Skip() | |
✔️ | SkipWhile() | |
✔️ | Sum() | |
✔️ | Take() | Returns a new collection. |
✔️ | TakeWhile() | Returns a new collection. |
ThenBy() | Sorting needs all data at once. | |
ThenByDescending() | Sorting needs all data at once. | |
✔️ | ToArray() | |
ToDictionary() | Not applicable. | |
ToList() | Not applicable. | |
ToLookup() | Not applicable. | |
Union() | Needs to store all previously processed data. | |
✔️ | Where() | |
✔️ | Zip() | Returns a new collection. |
Only non-lazy (or eager) methods are left out as they can't have a correct lazy implementation with generators. Sure, they could be implemented as standard deferred methods, but still, they'll want all the data at once.
For all inputs, keys are not exported nor used. If you absolutely need to access keys, consider storing them with the data.
Durability
This library is built to last. Whatever you throw at it, it should just work.
There are only few methods marked with a 💣 above that may throw an exception: .NET API requires that, so it was unavoidable. Other than that, you can expect only standard language errors to happen.