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
// the enumeration | |
object WeekDay extends Enumeration { | |
type WeekDay = Value | |
val Mon = Value("Monday") | |
val Tue = Value("Tuesday") | |
val Wed = Value("Wednesday") | |
val Thu = Value("Thursday") | |
val Fri = Value("Friday") | |
val Sat = Value("Saturday") | |
val Sun = Value("Sunday") |
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
// sample class with a double value | |
case class DoubleNanTest(price: Double) | |
// define typeclass instance for serialization | |
import dispatch.json._ | |
implicit val DoubleNanTestFormat: Format[DoubleNanTest] = new Format[DoubleNanTest] { | |
def reads(json: JsValue): DoubleNanTest = json match { | |
case JsString("Double.NaN") => DoubleNanTest(scala.Double.NaN) // special treatment for NaN | |
case JsNumber(n) => DoubleNanTest(n.doubleValue) | |
case _ => error("Invalid DoubleNanTest") |
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 definitions | |
trait SubUnit | |
case class Dept(name: String, manager: Employee, subUnits: List[SubUnit]) extends SubUnit | |
case class Employee(name: String, salary: Double) extends SubUnit | |
// typeclass instance | |
object SubUnitProtocol extends DefaultProtocol { | |
import JsonSerialization._ |
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 ElectricCar(b: Battery) { def batteryLevel = b.filledPercentage } | |
case class GasolineCar(g: GasTank) { def gasLevel = g.filledPercentage } | |
case class Battery(filledPercentage: Int) { def fill: Battery = Battery(100) } | |
case class GasTank(filledPercentage: Int) { def fill: GasTank = GasTank(100) } | |
trait Fills[C] { | |
def fill(car: C): 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
// problem with making it really optional in the general case .. | |
// what will be the output for the following ? | |
val str: Option[String] = None | |
tojson(str) // what ? | |
// hence .. | |
val str = None | |
fromjson[Option[String]](tojson[Option[String]](str)) should equal(str) |
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 deduplicate[T](l: List[T]): List[T] = { | |
def deduplicate_acc[T](l: List[T], acc: List[T]): List[T] = l match { | |
case x :: xs if acc contains x => deduplicate_acc(xs, acc) | |
case x :: xs => deduplicate_acc(xs, acc :+ x) | |
case Nil => acc | |
} | |
deduplicate_acc(l, List.empty[T]) | |
} | |
println(deduplicate(List(1,2,2,3,4,5,1,4,6,8,3))) // List(1, 2, 3, 4, 5, 6, 8) |
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 enumResultSet[E,A](rs: ResultSet, iter: IterV[E, A], get: ResultSet => IO[E]): IO[IterV[E, A]] = { | |
def loop(i: IterV[E, A]): IO[IterV[E, A]] = | |
i.fold(done = (_, _) => i.pure[IO], | |
cont = k => next(rs) >>= (hasMore => | |
if (!hasMore) i.pure[IO] | |
else get(rs) >>= (t => loop(k(El(t)))))) | |
loop(iter) | |
} |
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
// The "in" method returns an AnyRef instead of T in order to support anonymous de-serialization. There may be cases where | |
// you don't know the exact type of the object, but instead de-serialize it and use extractors. e.g. | |
val addr = new InternationalAddress("Market Street", "San Francisco", "956871", "USA") | |
it("should support anonymous deserialization and extraction") { | |
val a = serializer.in( | |
serializer.out(addr)) | |
// use extractors | |
val c = 'city ? str |
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
debasish@debasishg:~/my-projects/scouchdb$ sbt | |
[info] Building project scouchdb 0.6 against Scala 2.9.0 | |
[info] using SCouchDbProject with sbt 0.7.6.RC0 and Scala 2.7.7 | |
> compile | |
[info] | |
[info] == compile == | |
[info] Source analysis: 9 new/modified, 0 indirectly invalidated, 0 removed. | |
[info] Compiling main sources... | |
[info] == compile == | |
java.lang.Error: Unknown type: Map[_ >: ? <: ?, _ >: ? <: ?], <error> [class scala.tools.nsc.symtab.Types$ExistentialType, class scala.tools.nsc.symtab.Types$ErrorType$] TypeRef? false |
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
// classes and typeclass instances | |
case class WFNode(val id: Long, val name: String, val requestedBy: Option[String] = None) | |
implicit val WFNodeFormat: Format[WFNode] = | |
asProduct3("id", "name", "requestedBy")(WFNode)(WFNode.unapply(_).get) | |
case class ValueNode(override val id: Long, override val name: String, | |
override val requestedBy: Option[String] = None, | |
var value: Option[String] = None) extends WFNode(id, name, requestedBy) | |
implicit val ValueNodeFormat: Format[ValueNode] = |