Skip to content

Implement partition in a later release #3

@rotexdegba

Description

@rotexdegba
// laravel style
$collection = collect([1, 2, 3, 4, 5, 6]);
list($underThree, $aboveThree) = $collection->partition(function ($i) {
    return $i < 3;
});

// versatile emulation
\VersatileCollections\GenericCollection::makeNew(
    [1, 2, 3, 4, 5, 6]
)->reduce(
    function($carry, $item) {

        if(count($carry) <= 0) {
            $carry['above_three'] = new \VersatileCollections\GenericCollection();
            $carry['below_three'] = new \VersatileCollections\GenericCollection();
        }
        
        if( $item < 3 ) {
            $carry['below_three'][] = $item;
        } else {
            $carry['above_three'][] = $item;
        }
        return $carry;
    }, 
    new \VersatileCollections\GenericCollection()
);

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions