Skip to content
This repository has been archived by the owner on Jan 1, 2021. It is now read-only.
/ LazyLINQ Public archive

A lazy-only port of .NET LINQ for PHP 💥

License

Notifications You must be signed in to change notification settings

sanmai/LazyLINQ

Repository files navigation

Build Status Coverage Status Infection MSI Codacy Badge Maintainability

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.