Skip to content

Releases: sagifogel/Proptics

v0.3.0

11 Aug 07:58
Compare
Choose a tag to compare
  • addition of Scala 2.x macros for Lens/ALens and Prism/APrism (#112) @sagifogel
import proptics.Lens
import proptics.macros.GLens

final case class Person(name: String, address: Address)
final case class Address(city: String, street: Street)
final case class Street(name: String, number: Int)

val personNameLens: Lens[Person, Int] = GLens[Person](_.address.street.number)
val person = Person("Walter White", Address("Albuquerque", Street("Negra Arroyo Lane", 9)))

personNameLens.set(308)(person)
// Person(Walter White,Address(Albuquerque,Street(Negra Arroyo Lane,308)))
import proptics.Prsims
import proptics.macros.GPrism

sealed trait Request
final case class GET(path: List[String]) extends Request
final case class POST(path: List[String], body: String) extends Request

val webServerPrism = GPrism[Request, GET]

webServerPrism.preview(GET(List("path")))
//  Some(GET(List(path)))
webServerPrism.preview(POST(List("path"), "body"))
// None

v0.2.1

26 May 17:39
fbf8953
Compare
Choose a tag to compare

What's Changed

unsafePartsOf is similar to partsOf but can change the type of the focus

Traversal_[S, T, A, B] => Lens[S, T, List[A], List[B]]

Using unsafePartsOf we can set the elements of a List to a different type

import proptics.instances.partsOf._
import proptics.std.tuple._1P
import proptics.{Lens, Traversal}

val unsafePartsOfFromTraversal = 
  Traversal_.fromTraverse[List, (String, Int), (Boolean, Int)]
    .andThen(_1P[String, Boolean, Int])
    .unsafePartsOf

val target = List("A", "B", "C").zipWithIndex 
// List((A,0), (B,1), (C,2))

unsafePartsOfFromTraversal.set(List(true, false, true))(target)
// List((true,0), (false,1), (true,2))

It’s called unsafe, because it will crash if we set with the wrong number of elements

unsafePartsOfFromTraversal.set(List(true, false))(target)
// java.lang.IllegalArgumentException: Not enough elements were supplied

v0.2.0

23 May 23:02
Compare
Choose a tag to compare

What's Changed

partsOf converts a Traversal into a Lens over a list of the Traversal’s foci

Traversal[S, A] => Lens[S, List[A]]

Using partsOf we can set the elements of a List

import proptics.instances.field1._
import proptics.instances.partsOf._
import proptics.std.tuple._1
import proptics.syntax.traversal._
import proptics.{Lens, Traversal}

val partsOfFromTraversal = 
  Traversal.fromTraverse[List, (String, Int)]
     .andThen(_1[String, Int])
     .partsOf

val target = List("A", "B", "C").zipWithIndex 
// List((A,0), (B,1), (C,2))

partsOfFromTraversal.set(List("C", "A", "T"))(target)
// List((C,0), (A,1), (T,2))

v0.1.0

12 May 22:52
Compare
Choose a tag to compare
fix echo