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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/core/src/main/scala/higherkindness/droste/basis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Project.find[F, R, B](r)(pf)

def contains(r: R, c: R)(implicit R: Eq[R], F: Foldable[F]): Boolean =
Project.contains[F, R](r, c)

Expand Down Expand Up @@ -73,6 +76,9 @@ object Project extends FloatingBasisInstances[Project] {
pf.lift(_)
.foldRight[U](U.algebra(NilF))((a, b) => U.algebra(ConsF(a, b))))

def find[F[_], R, B](r: R)(pf: PartialFunction[R, B])(implicit P: Project[F, R], F: Foldable[F]): Option[B] =
foldMap[F, R, Option[B] @@ Tags.First](r)(pf.lift(_).first).unwrap

def contains[F[_], R](
r: R,
c: R)(implicit P: Project[F, R], R: Eq[R], F: Foldable[F]): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ object ProjectSyntax {
implicit U: Basis[ListF[B, ?], U]): U =
Project.collect[F, T, U, B](self)(pf)

def find[B](pf: PartialFunction[T, B]): Option[B] =
Project.find(self)(pf)

def contains(c: T)(implicit T: Eq[T]): Boolean =
Project.contains(self, c)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ object newtypes {
object Tags {
sealed trait Conjunction
sealed trait Disjunction
sealed trait First
}

implicit class BooleanOps(b: Boolean) {
Expand All @@ -34,4 +35,14 @@ object newtypes {
b: Boolean @@ Tags.Disjunction): Boolean @@ Tags.Disjunction =
@@(a.unwrap || b.unwrap)
}

implicit class OptionOps[B](o: Option[B]) {
def first: Option[B] @@ Tags.First = @@(o)
}

implicit def optionMonoid[B]: Monoid[Option[B] @@ Tags.First] =
new Monoid[Option[B] @@ Tags.First] {
def empty: Option[B] @@ Tags.First = @@(Option.empty[B])
def combine(x: Option[B] @@ Tags.First, y: Option[B] @@ Tags.First): Option[B] @@ Tags.First = @@(x.unwrap.orElse(y.unwrap))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package examples

import cats.instances.list._
import cats.kernel.Eq
import cats.syntax.option._

import org.scalacheck.Properties
import org.scalacheck.Prop._
Expand Down Expand Up @@ -44,6 +45,16 @@ final class FoldableOpsChecks
case Lam(name, _) => name
} ?= List("a", "b", "c", "d", "e", "f")

property("find none") =
tru[LExpr].find {
case v @ Var(name) if name.startsWith("d") => v
} ?= Option.empty[Var]

property("find existing") =
tru[LExpr].find {
case Lam(name, _) if !name.startsWith("a") => name
} ?= "b".some

property("any") =
tru[LExpr].any {
case Var(name) => true
Expand Down