Skip to content

Instantly share code, notes, and snippets.

@REDNBLACK
Last active June 14, 2019 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save REDNBLACK/94e15bfa189e37584092b29e2252c6cd to your computer and use it in GitHub Desktop.
Save REDNBLACK/94e15bfa189e37584092b29e2252c6cd to your computer and use it in GitHub Desktop.
package pkg
import cats.{Applicative, Traverse}
import monocle.macros.{GenLens, Lenses}
import monocle.macros.syntax.lens._
import monocle.function.all._
import cats.syntax.option._
import monocle.Traversal
import monocle.function.FilterIndex.fromTraverse
import monocle.function.{FilterIndex, FilterIndexFunctions}
object Demo extends App {
@Lenses
case class Company(name: String, employees: List[Employee])
@Lenses
case class Employee(name: String, data: List[Field])
@Lenses
case class Field(name: String, value: Option[String])
val company1 = Company("awesome inc", List(
Employee("Steven", Field("age", "20".some) :: Nil),
Employee("Carl", Field("age", "30".some) :: Nil),
Employee("Max", Field("married", "true".some) :: Nil)
))
object Impl {
abstract class Filter[S, A] extends Serializable {
def filter(predicate: A => Boolean): Traversal[S, A]
}
trait FilterFunctions {
def filter[S, A](predicate: A => Boolean)(implicit ev: Filter[S, A]): Traversal[S, A] = ev.filter(predicate)
def filterNot[S, A](predicate: A => Boolean)(implicit ev: Filter[S, A]): Traversal[S, A] = ev.filter(!predicate(_))
}
object Filter extends FilterFunctions {
import cats.syntax.traverse._
import cats.syntax.applicative._
implicit def traverseFilter[S[_]: Traverse, A]: Filter[S[A], A] = predicate => new Traversal[S[A], A] {
def modifyF[F[_]: Applicative](f: A => F[A])(s: S[A]): F[S[A]] =
s.traverse(a => if (predicate(a)) f(a) else a.pure[F])
}
}
}
import Impl.Filter._
import cats.instances.list._
val lens = GenLens[Company](_.employees) ^|->> each ^|-> GenLens[Employee](_.data) ^|->> filter((_: Field).name == "age") modify(
_.copy(value = "100500".some)
)
println(company1)
println(lens(company1))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment