This file contains hidden or 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 hidden or 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 hidden or 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
| # vecs: an array of real vectors | |
| def crp(vecs): | |
| clusterVec = [] # tracks sum of vectors in a cluster | |
| clusterIdx = [] # array of index arrays. e.g. [[1, 3, 5], [2, 4, 6]] | |
| ncluster = 0 | |
| # probablity to create a new table if new customer | |
| # is not strongly "similar" to any existing table | |
| pnew = 1.0/ (1 + ncluster) | |
| N = len(vecs) | |
| rands = random.rand(N) # N rand variables sampled from U(0, 1) |
This file contains hidden or 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 hidden or 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", "[email protected]", new LocalDate("1952-03-11"))) | |
| // => {"id":1,"firstName":"Arthur","lastName":"Dent",birthDate":"1952-03-11"} |
This file contains hidden or 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 hidden or 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 idFormat[T]: Format[Id[T]] = Format( | |
| __.read[Long].map(Id(_)), | |
| new Writes[Id[T]]{ def writes(o: Id[T]) = JsNumber(o.id) } | |
| ) | |
| implicit val personIdFormat = idFormat[Person] | |
| implicit val nameFormat = Json.format[Name] | |
| implicit val personFormat = Json.format[Person] | |
| Json.toJson(Person(Some(Id(1)), Name("Arthur","Dent"), "[email protected]", new LocalDate("1952-03-11"))) | |
| // => {"id":1,"name":{"firstName":"Arthur","lastName":"Dent"},"email":"[email protected]","birthDate":"1952-03-11"} |
This file contains hidden or 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 hidden or 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 hidden or 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("""{ |
OlderNewer