Filter and Sort arbitrary Collections
inkrement opened this issue · 5 comments
Is it possible to apply multiple filters on arbitrary Collections of this package? I just found a solution to apply a single filter (should be possible by using CallbackFilterIterator
).
And is there a preferred or standardised way to sort a collection of objects by a specific attribute?
I will think about this a bit and respond later. I just wanted you to know your questions haven't gone unnoticed :)
In a few days I should have a PR that adds a filter algorithm and interface that should work with just about anything meaningful. It would allow you to do something like $set->filter($callback)->filter($callback)
.
As for sort: I'm not sure on that one. Efficient sorting algorithms impose certain restrictions that make it difficult to generalize. Depending on what you want to sort you may want to use one of the Sorted*
structures, such as SortedSet
or SortedMap
.
Ok thanks! Will the filter interface only support sets, or all collections?
The semantics of the Filter
interface is that by implementing it you are claiming you can have a better implementation than the generic version that works on all Traversable
objects. So only some of the classes will implement it such as sets and maps. There is no advantage of implementing it inside the Stack
, for instance.
The filter
function will work on any array
or Traversable
and will delegate to the Filter::filter
method if applicable. An example that would work with an array
or instance of Traversable
or Ardent\Attribute\Filter
:
use function Ardent\Algorithm\filter;
$filtered = filter($cb1, filter($cb2, $data));
Sounds great!