Skip to content

Instantly share code, notes, and snippets.

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)
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
@ryanpbrewster
ryanpbrewster / From word2vec to doc2vec: similarity driven CRP, by Yingjie Miao.py
Last active January 29, 2018 12:38
From word2vec to doc2vec --- similarity driven CRP by Yingjie Miao
# 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)
@ryanpbrewster
ryanpbrewster / Working with JSON in Play 2.1: Person, by Greg Methvin.scala
Created May 2, 2016 20:59
Definition of the case class Person from Working with JSON in Play 2.1 by Greg Methvin
case class Person(
id: Option[Long] = None,
firstName: String,
lastName: String,
birthDate: LocalDate)
@ryanpbrewster
ryanpbrewster / Writes[Person] for Working with JSON in Play 2.1 by Greg Methvin.scala
Created May 2, 2016 21:01
Writes[Person] for Working with JSON in Play 2.1 by Greg Methvin
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"}
@ryanpbrewster
ryanpbrewster / Complex case class Person for Working with JSON in Play 2.1 by Greg Methvin.scala
Created May 2, 2016 21:03
Complex case class Person for Working with JSON in Play 2.1 by Greg Methvin
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,
@ryanpbrewster
ryanpbrewster / Complex Writes[Person] for Working with JSON in Play 2.1 by Greg Methvin.scala
Last active May 6, 2016 20:45
Complex Writes[Person] for Working with JSON in Play 2.1 by Greg Methvin
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"}
@ryanpbrewster
ryanpbrewster / Functional Format[Person] for Working with JSON in Play 2.1 by Greg Methvin.scala
Created May 2, 2016 21:10
Functional Format[Person] for Working with JSON in Play 2.1 by Greg Methvin
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))
@ryanpbrewster
ryanpbrewster / Format[Person] using orElse for Working with JSON in Play 2.1 by Greg Methvin.scala
Created May 2, 2016 21:12
Format[Person] using orElse for Working with JSON in Play 2.1 by Greg Methvin
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
@ryanpbrewster
ryanpbrewster / Format[Person] using defaults for Working with JSON in Play 2.1 by Greg Methvin.scala
Last active May 2, 2016 21:17
Format[Person] using defaults for Working with JSON in Play 2.1 by Greg Methvin.scala
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("""{