monoclelib
package monoclelib
- Alphabetic
- Public
- Protected
Value Members
- object IsoExercises extends AnyFlatSpec with Matchers with Section
An
Iso
is an optic which converts elements of typeS
into elements of typeA
without loss.Iso
An
Iso
is an optic which converts elements of typeS
into elements of typeA
without loss.Consider a case class
Person
with two fields:case class Person(name: String, age: Int)
- object IsoHelper
- object LensExercises extends AnyFlatSpec with Matchers with Section
A Lens is an optic used to zoom inside a
Product
, e.g.Lens
A Lens is an optic used to zoom inside a
Product
, e.g.case class
,Tuple
,HList
or evenMap
.Lenses
have two type parameters generally calledS
andA
:Lens[S, A]
whereS
represents theProduct
andA
an element inside ofS
.Let’s take a simple case class with two fields:
case class Address(strNumber: Int, streetName: String)
We can create a
Lens[Address, Int]
which zooms from anAddress
to its fieldstrNumber
by supplying a pair of functions:get: Address => Int
set: Int => Address => Address
import monocle.Lens val strNumber = Lens[Address, Int](_.strNumber)(n => a => a.copy(strNumber = n))
This case is really straightforward so we automated the generation of
Lenses
from case classes using a macro:import monocle.macros.GenLens val strNumber = GenLens[Address](_.strNumber)
- object LensHelper
- object MonocleLib extends Library
Monocle is an optics library for Scala (and Scala.js) strongly inspired by Haskell Lens.
- object OptionalExercises extends AnyFlatSpec with Matchers with Section
An
Optional
is an Optic used to zoom inside aProduct
, e.g.Optional
An
Optional
is an Optic used to zoom inside aProduct
, e.g.case class
,Tuple
,HList
or even Map. Unlike theLens
, the element that theOptional
focuses on may not exist.Optionals
have two type parameters generally calledS
andA
:Optional[S, A]
whereS
represents theProduct
andA
an optional element inside ofS
.Let’s take a simple list with integers.
We can create an
Optional[List[Int], Int]
which zooms from aList[Int]
to its potential head by supplying a pair of functions:-
getOption: List[Int] => Option[Int]
-
set: Int => List[Int] => List[Int]
import monocle.Optional val head = Optional[List[Int], Int] { case Nil => None case x :: xs => Some(x) }{ a => { case Nil => Nil case x :: xs => a :: xs } }
-
- object OptionalHelper
- object PrismExercises extends AnyFlatSpec with Matchers with Section
A
Prism
is an optic used to select part of aSum
type (also known asCoproduct
), e.g.Prism
A
Prism
is an optic used to select part of aSum
type (also known asCoproduct
), e.g.sealed trait
orEnum
.Prisms
have two type parameters generally calledS
andA
:Prism[S, A]
whereS
represents theSum
andA
a part of the Sum.Let’s take a simplified
Json
encoding:sealed trait Json case object JNull extends Json case class JStr(v: String) extends Json case class JNum(v: Double) extends Json case class JObj(v: Map[String, Json]) extends Json
We can define a
Prism
which only selectsJson
elements built with aJStr
constructor by supplying a pair of functions: -getOption: Json => Option[String]
-reverseGet (aka apply): String => Json
import monocle.Prism val jStr = Prism[Json, String]{ case JStr(v) => Some(v) case _ => None }(JStr)
It is common to create a
Prism
by pattern matching on constructor, so we also addedpartial
which takes aPartialFunction
:val jStr = Prism.partial[Json, String]{case JStr(v) => v}(JStr)
- object PrismHelper
- object TraversalExercises extends AnyFlatSpec with Matchers with Section
A
Traversal
is the generalisation of anOptional
to several targets.Traversal
A
Traversal
is the generalisation of anOptional
to several targets. In other word, aTraversal
allows to focus from a typeS
into 0 to n values of typeA
.The most common example of a
Traversal
would be to focus into all elements inside of a container (e.g.List
,Vector
,Option
). To do this we will use the relation between the typeclasscats.Traverse
andTraversal
:import monocle.Traversal import cats.implicits._ // to get all cats instances including Traverse[List] val xs = List(1,2,3,4,5)
- object TraversalHelper