Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce yield_groups() #46

Merged
merged 1 commit into from
Aug 31, 2023
Merged

Introduce yield_groups() #46

merged 1 commit into from
Aug 31, 2023

Conversation

lippserd
Copy link
Member

@lippserd lippserd commented Aug 10, 2023

This PR introduces yield_groups() to yield sets of items from a sorted iterator grouped by a specific criterion gathered from a callback:

namespace ipl\Html\Example;

use Generator;
use Iterator;

use function ipl\Stdlib\yield_groups;

class YieldGroupsExample
{
    /**
     * @param Iterator<int, object{category: int, id: string, data: string}> $data
     *
     * @return Generator<int, array<int, object{category: int, id: string, data: string}>>
     */
    public function groupByCategory(Iterator $data): Generator
    {
        return yield_groups($data, function ($v): int {
            return $v->category;
        });
    }

    /**
     * @param Iterator<int, object{category: int, id: string, data: string}> $data
     *
     * @return Generator<int, array<int, string>>
     */
    public function groupByCategoryWithDataAsValue(Iterator $data): Generator
    {
        return yield_groups($data, function ($v): array {
            return [$v->category, $v->data];
        });
    }

    /**
     * @param Iterator<int, object{category: int, id: string, data: string}> $data
     *
     * @return Generator<int, array<string, string>>
     */
    public function groupByCategoryWithDataAsValueAndIdAsKey(Iterator $data): Generator
    {
        return yield_groups($data, function ($v): array {
            return [$v->category, $v->data, $v->id];
        });
    }
}

@cla-bot cla-bot bot added the cla/signed label Aug 10, 2023
@lippserd lippserd force-pushed the yield_groups branch 2 times, most recently from 1627690 to fdc9df6 Compare August 10, 2023 08:58
@lippserd lippserd marked this pull request as ready for review August 10, 2023 08:58
@lippserd lippserd requested review from nilmerg and yhabteab August 10, 2023 08:59
src/functions.php Outdated Show resolved Hide resolved
tests/FunctionsTest.php Show resolved Hide resolved
tests/FunctionsTest.php Outdated Show resolved Hide resolved
@lippserd lippserd force-pushed the yield_groups branch 3 times, most recently from ea5955a to 7ea16db Compare August 10, 2023 10:40
@lippserd lippserd requested a review from yhabteab August 10, 2023 10:42
yhabteab
yhabteab previously approved these changes Aug 10, 2023
Copy link
Member

@yhabteab yhabteab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nilmerg
Copy link
Member

nilmerg commented Aug 25, 2023

I've just fixed the phpstan complaints: https://github.com/Icinga/ipl-stdlib/actions/runs/5975237958

@nilmerg nilmerg added the enhancement New feature or request label Aug 25, 2023
@nilmerg nilmerg added this to the v0.13.0 milestone Aug 25, 2023
@nilmerg nilmerg merged commit 1534be2 into main Aug 31, 2023
@nilmerg nilmerg deleted the yield_groups branch August 31, 2023 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla/signed enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants