Skip to content

Instantly share code, notes, and snippets.

View debasishg's full-sized avatar
🏠
Working from home

Debasish Ghosh debasishg

🏠
Working from home
View GitHub Profile
// 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")
@debasishg
debasishg / gist:887058
Created March 25, 2011 15:48
Serialization of Double.NaN
// 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")
// 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._
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
// 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)
@debasishg
debasishg / gist:911533
Created April 9, 2011 16:37
de-duplicate
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)
@debasishg
debasishg / gist:924143
Created April 17, 2011 15:36
@runanorama's code snippet for using iteratee based JDBC results processing
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)
}
// 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
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
// 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] =