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
data Genre = Horror | Comedy deriving (Show) | |
data HorrorAdjective = Scary | Boring deriving (Show) | |
data ComedyAdjective = Funny | Lame deriving (Show) | |
data Adjective = HA HorrorAdjective | CA ComedyAdjective deriving (Show) | |
data Movie = Movie String Genre deriving (Show) | |
movieDescriptions = [ (Movie "Airplane" Comedy, CA Funny) | |
, (Movie "Jaws" Horror, HA Scary) |
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
class Foo { | |
sealed trait A | |
case class A1(x: Int) extends A | |
case class A2(y: String) extends A | |
case class A3(z: Double) extends A | |
sealed trait B | |
case class B1(z: Int) extends B | |
case class B2(z1: Int) extends B |
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 Person( | |
id: Option[Long] = None, | |
firstName: String, | |
lastName: String, | |
birthDate: LocalDate) |
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 play.api.libs.json._ | |
implicit val personFormat = Json.format[Person] | |
Json.toJson(Person(Some(1), "Arthur", "Dent", "arthur@example.com", new LocalDate("1952-03-11"))) | |
// => {"id":1,"firstName":"Arthur","lastName":"Dent",birthDate":"1952-03-11"} |
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 Id[T](id: Long) { // in Id library | |
override def toString = id.toString | |
} | |
case class Name(firstName: String, lastName: String) { | |
def firstLast = s"$firstName $lastName" | |
def lastFirst = s"$lastName, $firstName" | |
} | |
case class Person( | |
id: Option[Id[Person]] = None, | |
name: Name, |
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 play.api.libs.functional.syntax._ | |
implicit val personFormat: Format[Person] = ( | |
(__ "id").formatNullable[Id[Person]] and | |
( | |
(__ "firstName").format[String] and | |
(__ "lastName").format[String] | |
)(Name.apply, unlift(Name.unapply)) and | |
(__ "email").format(Reads.email) and | |
(__ "birthDate").format[LocalDate] | |
)(Person.apply, unlift(Person.unapply)) |
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 personFormatOld: Format[Person] = ??? // old serializer | |
val personFormat: Format[Person] = ??? // new serializer | |
implicit val personReads: Reads[Person] = personFormat orElse personFormatOld | |
implicit val personWrites: Writes[Person] = personFormat |
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 play.api.libs.json._ | |
val pattern = "yyyy-MM-dd'T'HH:mm:ssz" | |
implicit val dateFormat = Format[DateTime]( | |
Reads.jodaDateReads(pattern), | |
Writes.jodaDateWrites(pattern) | |
) | |
Json.toJson(new DateTime("1952-03-11")) | |
// => "1952-03-11T00:00:00PDT" |
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
implicit val personFormat: Format[Person] = ( | |
(__ "id").formatNullable[Id[Person]] and | |
(__ "name").format[Name] and | |
(__ "email").formatNullable( | |
Format(Reads.email, Writes.StringWrites)) | |
.inmap[String](_.getOrElse(""), Some(_).filterNot(_.isEmpty) | |
) and | |
(__ "birthDate").format[LocalDate] | |
)(Person.apply, unlift(Person.unapply)) | |
Json.fromJson[Person](Json.parse("""{ |
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 sort[T <% Ordered[T]](seq: Seq[T]): Seq[T] = seq match { | |
case Seq() => Seq() | |
case x +: xs => | |
val (before, after) = xs partition (_ < x) | |
sort(before) ++ sort(x +: after) | |
} | |
sort(IndexedSeq("Greg", "Eishay", "Andrew")) | |
// => List(Andrew, Eishay, Greg) |
OlderNewer