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

Feature proposal: add new function \iter\tap #96

Closed
athrawes opened this issue Sep 1, 2023 · 1 comment
Closed

Feature proposal: add new function \iter\tap #96

athrawes opened this issue Sep 1, 2023 · 1 comment

Comments

@athrawes
Copy link
Contributor

athrawes commented Sep 1, 2023

Quite often when I'm in the middle of a pipeline of iterators, I would like to examine the current values coming through the pipeline without modifying them.

For example, I might want to log an intermediate result of the pipeline. One could abuse \iter\map to take items from the iterator, perform side effects, and return the items without modification; however, this is somewhat unwieldy:

$_ = \iter\map(doSomething(...), $queue);
$_ = \iter\map(
    function ($item) use ($logger) {
        $logger->info("did something with $item");
        return $item;
    },
    $_
);
$_ = \iter\map(doSomethingWithPreviousValues(...), $_);

As such, I'd like to propose a new function \iter\tap() which takes a callback and an iterable.
For each item in the iterable, it would call the callback with the item but return the item as passed to tap.

$_ = \iter\map(doSomething(...), $queue);
$_ = \iter\tap(fn ($item): void => $logger->info("did something with $item"), $_);
$_ = \iter\map(doSomethingWithPreviousValues(...), $_);

This would be similar in spirit to RxJS's tap function or the Java Stream.peek method

See #95

@nikic
Copy link
Owner

nikic commented Dec 10, 2023

This has been implemented in #95, and I just created a release for it, so closing this issue.

@nikic nikic closed this as completed Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants