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

refactor: Update Transpose operation in point free style. #178

Merged
merged 2 commits into from
Aug 22, 2021

Conversation

drupol
Copy link
Collaborator

@drupol drupol commented Aug 15, 2021

This PR:

  • Update Transpose in point free style.

@drupol drupol added the enhancement New feature or request label Aug 15, 2021
@drupol drupol force-pushed the refactor/update-transpose-operation branch 2 times, most recently from dc01e86 to 48f555c Compare August 16, 2021 18:11
@drupol drupol marked this pull request as ready for review August 16, 2021 20:13
@drupol drupol force-pushed the refactor/update-transpose-operation branch from 48f555c to 4798ce7 Compare August 16, 2021 21:14
@drupol drupol enabled auto-merge (squash) August 17, 2021 21:21
Comment on lines -33 to -40
return
$callbackForKeys =
/**
* @param Iterator<TKey, T> $iterator
* @param array $carry
* @param non-empty-array<int, TKey> $key
*
* @return Generator<TKey, list<T>>
* @return TKey
*/
static function (Iterator $iterator): Generator {
$mit = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm removing this closure makes the operation impure now because of the MultipleIterator instantiation. Before this instantiation happened in this returned closure, which meant that the operation itself was still pure, even though it returned an impure closure.

Is there any way we can keep this behaviour in the refactor?

Copy link
Collaborator Author

@drupol drupol Aug 19, 2021

Choose a reason for hiding this comment

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

Indeed. I asked the question on Psalm's slack channel and they told me to use a stub. I tried, but I'm going nowhere.
I wish I could find a proper way without altering the psalm baseline.

However, in this particular case I think we can safely ignore the issue.

  1. First, because the Transpose operation will never return a MultipleIterator.
  2. Second, because the Transpose operation is using a MultipleIterator object to determine the result. This object is an intermediary variable(tool) that is used to find the resulting solution.

I will set this PR as draft in the meantime, I'll try to come up with a better solution.

Copy link
Collaborator Author

@drupol drupol Aug 19, 2021

Choose a reason for hiding this comment

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

I quickly drafted a simpler example here: https://3v4l.org/LNuDT

Despite the fact that I use a MultipleIterator, this is completely pure.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@drupol what I meant here is this: can we keep the same behaviour where we use the MultipleIterator in a Closure? I know it's not really an issue and it's safe to ignore, I'm just wondering if it can be done.

Something like:

public function __invoke(): Closure 
{
    return 
        static function () {
            $pipe = ... // this might have to be a bit different

            return $pipe; // might need to call it here e.g. $pipe();
        }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Going to do a bit more research to fulfill your request and I'll let you know asap.

@drupol drupol marked this pull request as draft August 19, 2021 08:43
auto-merge was automatically disabled August 19, 2021 08:43

Pull request was converted to draft

Based on suggestion from @weirdan on Slack.

> See https://psalm.dev/r/cc0354c625 and https://3v4l.org/LNuDT for simple examples.

And Bruce's suggestion:
> It'll still look pure to the outside: https://psalm.dev/r/6a4e6f8dde
@drupol drupol force-pushed the refactor/update-transpose-operation branch from e73270c to cd5e4d2 Compare August 19, 2021 09:34
@drupol drupol marked this pull request as ready for review August 19, 2021 13:23
@drupol drupol enabled auto-merge (squash) August 19, 2021 17:14
@drupol drupol merged commit d27116a into master Aug 22, 2021
@drupol drupol deleted the refactor/update-transpose-operation branch August 22, 2021 20:50
@@ -26,46 +26,45 @@ final class Transpose extends AbstractOperation
/**
* @pure
*
* @psalm-suppress ImpureMethodCall
Copy link
Collaborator

Choose a reason for hiding this comment

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

if we do end up keeping this we should always have clarifying comments for them, e.g.

@psalm-suppress ImpureMethodCall - using MultipleIterator as an internal tool which is not returned

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just see this message! Good point, going to make the change.

drupol added a commit that referenced this pull request Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants