NSPL
NSPL copied to clipboard
Add partitionBy to split into arbitrary amounts of partitions
This adds support for partitionBy
, to allow partitioning into more than two partitions. This can be useful if you have a list of things that you want to group by (groupBy
could be made an alias of this) a specific property or other criteria - the sky is the limit with callback functions 😃.
partitionBy
behaves the same as partition
if you use a true unary predicate (something that returns a boolean based on the argument), but allows other functions that return different types than booleans.
Currently only int
and string
return values are allowed, since that's what arrays support as keys, but in the future you could also allow returning Comparable objects or arbitrary objects and use something like SplObjectStorage
.
Thinking more on this, I see partition
could also support other callables as well, since the functions are near identical, except for treatment of the callback return value. partitionBy
with the same callback as partition
will also behave in the same way, that is, if the callback returns a boolean as is intended.
On the other hand, this would be backwards incompatible, since you can now pass callbacks to partition
that pass something else than booleans and they will be converted for you; using partitionBy
will result in different behavior.