p

monoclelib

package monoclelib

Ordering
  1. Alphabetic
Visibility
  1. Public
  2. Protected

Value Members

  1. object IsoExercises extends AnyFlatSpec with Matchers with Section

    An Iso is an optic which converts elements of type S into elements of type A without loss.

    Iso

    An Iso is an optic which converts elements of type S into elements of type A without loss.

    Consider a case class Person with two fields:

    case class Person(name: String, age: Int)
  2. object IsoHelper
  3. 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 even Map.

    Lenses have two type parameters generally called S and A: Lens[S, A] where S represents the Product and A an element inside of S.

    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 an Address to its field strNumber 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)
  4. object LensHelper
  5. object MonocleLib extends Library

    Monocle is an optics library for Scala (and Scala.js) strongly inspired by Haskell Lens.

  6. object OptionalExercises extends AnyFlatSpec with Matchers with Section

    An Optional is an Optic used to zoom inside a Product, e.g.

    Optional

    An Optional is an Optic used to zoom inside a Product, e.g. case class, Tuple, HList or even Map. Unlike the Lens, the element that the Optional focuses on may not exist.

    Optionals have two type parameters generally called S and A: Optional[S, A] where S represents the Product and A an optional element inside of S.

    Let’s take a simple list with integers.

    We can create an Optional[List[Int], Int] which zooms from a List[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
       }
    }
  7. object OptionalHelper
  8. object PrismExercises extends AnyFlatSpec with Matchers with Section

    A Prism is an optic used to select part of a Sum type (also known as Coproduct), e.g.

    Prism

    A Prism is an optic used to select part of a Sum type (also known as Coproduct), e.g. sealed trait or Enum.

    Prisms have two type parameters generally called S and A: Prism[S, A] where S represents the Sum and A 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 selects Json elements built with a JStr 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 added partial which takes a PartialFunction:

    val jStr = Prism.partial[Json, String]{case JStr(v) => v}(JStr)
  9. object PrismHelper
  10. object TraversalExercises extends AnyFlatSpec with Matchers with Section

    A Traversal is the generalisation of an Optional to several targets.

    Traversal

    A Traversal is the generalisation of an Optional to several targets. In other word, a Traversal allows to focus from a type S into 0 to n values of type A.

    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 typeclass cats.Traverse and Traversal:

    import monocle.Traversal
    import cats.implicits._   // to get all cats instances including Traverse[List]
    
    val xs = List(1,2,3,4,5)
  11. object TraversalHelper

Ungrouped