-
-
Save terjesb/1988071 to your computer and use it in GitHub Desktop.
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
// https://github.com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/Lens.scala | |
case class Lens[A,B](get: A => B, set: (A,B) => A) { | |
def apply(a: A) = get(a) | |
// ... | |
} |
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
// https://github.com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/Lens.scala | |
def mod(a:A, f: B => B) : A = set(a, f(get(a))) |
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
// https://github.com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/Lens.scala | |
val fst = Lens[(A,B),A](_._1, (ab,a) => (a,ab._2)) | |
fst.get((1, 2)) // 1 | |
fst(("a", 2)) // "a" | |
fst.set((1, 2), 3) // (3, 2) |
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
case class ToyBox(toys: Map[String, Toy]) |
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
sealed trait ToyCondition | |
case object New extends ToyCondition // toy has just been bought or rarely played with | |
case object Used extends ToyCondition // toy has obviously been played with | |
case object Destroyed extends ToyCondition // teeth have really sunk in, its trash time | |
case class Toy(condition: ToyCondition, daysOld: Int) |
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
def updateCondition(toyBox: ToyBox, name: String, newCondition: ToyCondition): ToyBox |
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
def updateCondition(toyBox: ToyBox, name: String, newCondition: ToyCondition): ToyBox = { | |
toyBox.toys.get(name) | |
.map(t => toyBox.copy(toys = toyBox.toys + (name -> t.copy(condition = newCondition)))) | |
.getOrElse(toyBox) | |
} |
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
val toys: Lens[ToyBox, Map[String, Toy]] = Lens(_.toys, (tb,ts) => tb copy (toys = ts)) |
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
val condition: Lens[Toy, Condition] = Lens(_.condition, (t,c) => t copy (condition = c)) |
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
def updateCondition(toyBox: ToyBox, name: String, newCondition: ToyCondition): ToyBox = { | |
toyBox.toys.get(name) | |
.map(t => toys.set(toyBox, toyBox.toys + (name -> condition.set(t, newCondition))) | |
.getOrElse(toyBox) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment