cats.mtl
FunctorEmpty[F] allows you to map and filter out elements simultaneously.
FunctorEmpty[F]
map
FunctorEmpty has two external laws:
FunctorEmpty
def mapFilterComposition[A, B, C](fa: F[A], f: A => Option[B], g: B => Option[C]) = { val lhs: F[C] = fa.mapFilter(f).mapFilter(g) val rhs: F[C] = fa.mapFilter(a => f(a).flatMap(g)) lhs <-> rhs } def mapFilterMapConsistency[A, B](fa: F[A], f: A => B) = { fa.mapFilter(f andThen (_.some)) <-> fa.map(f) }
FunctorEmpty has three internal laws:
def collectConsistentWithMapFilter[A, B](fa: F[A], f: PartialFunction[A, B]) = { collect(fa)(f) <-> mapFilter(fa)(f.lift) } def flattenOptionConsistentWithMapFilter[A](fa: F[Option[A]]) = { flattenOption(fa) <-> mapFilter(fa)(identity) } def filterConsistentWithMapFilter[A](fa: F[A], f: A => Boolean) = { filter(fa)(f) <-> mapFilter(fa)(a => if (f(a)) Some(a) else None) }
FunctorEmpty[F]
allows you tomap
and filter out elements simultaneously.FunctorEmpty
has two external laws:FunctorEmpty
has three internal laws: