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

[data] optimize Maybe.collect #1083

Merged
merged 3 commits into from
Mar 6, 2025

Conversation

matteobilardi
Copy link
Contributor

Problem

The current implementation of Maybe.collect checks if the partial function is defined on the input value before calling it. This can be sub-optimal, as it causes a double evaluation of pattern matchers and guards in the partial function (once on the call to pf.isDefinedAt(value) and once on the application pf(value).

Solution

Using the applyOrElse method allows optimized implementations of PartialFuncion, such as partial function literals (mentioned in https://github.com/scala/scala/blob/780d80c714354871bc5972045926b7ccd042e102/src/library/scala/PartialFunction.scala#L194) to avoid double evaluations of guards.

@matteobilardi matteobilardi marked this pull request as ready for review March 2, 2025 22:30
@fwbrasil
Copy link
Collaborator

fwbrasil commented Mar 5, 2025

Welcome, and thanks for the contribution! :)

@fwbrasil fwbrasil merged commit e80a9f4 into getkyo:main Mar 6, 2025
3 checks passed
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

Successfully merging this pull request may close these issues.

3 participants