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

Add find operation to FoldableOps #118

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bilki
Copy link

@bilki bilki commented Jul 4, 2019

This adds the find operation to FoldableOps, to match the first element of the structure for which the partial function passed as argument is defined.

I'm not sure if this is the correct semantic of find operation for a given R recursive type. I have another implementation taking a boolean predicate like any, or all operations do, but then the result type of find changes from Option[B] to Option[R], returning the whole substructure from where the first match succeeded.

I also had to add a new tag First and the Option monoid in order to use foldMap. Another concern is that I don't know if this shortcircuits on the first match (does any operation shortcircuit?).

Any feedback is more than welcome, as this is my first time working with recursion schemes concepts.

@@ -42,6 +42,9 @@ trait Project[F[_], R] { self =>
F: Foldable[F]): U =
Project.collect[F, R, U, B](r)(pf)

def find[B](r: R)(pf: PartialFunction[R, B])(implicit F: Foldable[F]): Option[B] =
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer the Boolean variation, as it's more consistent with find methods in the Scala stdlib.

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.

None yet

2 participants