Created
April 24, 2022 10:04
-
-
Save hiramekun/059c9ded4c44f10938936c66b8fbceb6 to your computer and use it in GitHub Desktop.
Lens Pattern with Monocle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import monocle.Monocle.toAppliedFocusOps | |
case class Country(name: String, code: String) | |
case class City(name: String, country: Country) | |
case class Address(number: Int, street: String, city: City) | |
case class Company(name: String, address: Address) | |
case class User(name: String, company: Company, address: Address) | |
object MonocleUserExample extends App { | |
val uk = Country("United Kingdom", "uk") | |
val london = City("London", uk) | |
val buckinghamPalace = Address(1, "Buckingham Palace Road", london) | |
val castleBuilders = Company("Castle Builders", buckinghamPalace) | |
val switzerland = Country("Switzerland", "CH") | |
val geneva = City("geneva", switzerland) | |
val genevaAddress = Address(1, "Geneva Lake", geneva) | |
val ivan = User("Ivan", castleBuilders, genevaAddress) | |
println(ivan) | |
val ivanFixed = ivan.focus(_.company.address.city.country.code).modify(_.toUpperCase) | |
println(ivanFixed) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment