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
stringPrism.modifyOption(_.toUpperCase)(JStr("some")) | |
// res1: Option[com.example.Json] = Some(JStr(SOME)) | |
stringPrism.modifyOption(_.toUpperCase)(JNull) | |
// res2: Option[com.example.Json] = None |
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
stringPrism.modify(_.toUpperCase)(JNull) | |
//res1: Json = JNull |
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
stringPrism.modify(_.toUpperCase) | |
//res1: com.example.Json => com.example.Json |
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 someJson: Json = JStr("someString") | |
val withPrism = stringPrism.modify(_.toUpperCase)(someJson) | |
// res1: com.example.Json = JStr(SOMESTRING) |
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 someJson: Json = JStr("someString") | |
someJson match { | |
case JStr(s) => JStr(s.toUpperCase) | |
case anythingElse => anythingElse | |
} | |
// res1: com.example.Json = JStr(SOMESTRING) |
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
stringPrism.getOption(JNull) | |
// res4: Option[String] = None |
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
stringPrism.getOption(JStr("someString")) | |
// res1: Option[String] = Some(someString) | |
stringPrism.reverseGet("someString") | |
// res2: com.example.Json = JStr(someString) |
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
// Based directly on definition: | |
val stringPrism = Prism[Json, String]{ | |
case JStr(v) => Some(v) | |
case _ => None | |
}(str => JStr(str)) | |
// The same prism but defined using `partial` helper and succinct syntax for second argument: | |
val stringPrism = Prism.partial[Json, String]{case JStr(v) => v}(JStr) |
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 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 |
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 toUpperCase: String => String = _.toUpperCase | |
val modifyStreetName: Person => Person = streetName.modify(toUpperCase) |
NewerOlder