Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active August 29, 2015 14:06
Show Gist options
  • Save amirrajan/78148ab6f98a21b1d9b6 to your computer and use it in GitHub Desktop.
Save amirrajan/78148ab6f98a21b1d9b6 to your computer and use it in GitHub Desktop.
Lenses, Scalaz and Scala
val firstEventL = Lens.lensu[EventContainer, Event](
(container, value) => container.copy(events = Seq(value) ++ container.events.tail), _.events.head)
val profileL = Lens.lensu[Event, Profile](
(event, value) => event.copy(profile = value), _.profile)
val reservationL = Lens.lensu[Profile, Reservation](
(profile, value) => profile.copy(reservation = value), _.reservation)
val settingAllProperties =
firstEventL =>=
{ _.copy(eventTitle = "New Title") } andThen
firstEventL >=> profileL =>=
{ _.copy(profileName = "New Profile Name") } andThen
firstEventL >=> profileL >=> reservationL =>=
{
_.copy(
reservationComments = "New Comments",
spaceReservations = Seq(
SpaceReservation(1111),
SpaceReservation(2222)))
} apply ec
val settingJustOneProperty =
firstEventL >=> profileL >=> reservationL =>=
{ _.copy(reservationComments = "New Comments") } apply ec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment