Skip to content

Instantly share code, notes, and snippets.

// Run this with scala <filename>
/**
* A Two-phase commit Monad
*/
trait Transaction[+T] {
def map[U](fn: T => U): Transaction[U] = flatMap { t => Constant(fn(t)) }
def flatMap[U](fn: T => Transaction[U]): Transaction[U] =
FlatMapped(this, fn)
@ppurang
ppurang / livescript.html
Created August 14, 2013 07:43
Livescript on the client - an html file and a test script
<!DOCTYPE html>
<html lang="en">
<head>
<title>livescript</title>
</head>
<body>
<h1>
Livescript - check the browser's console
</h1>
<h2>
@ppurang
ppurang / gist:5768047
Last active December 18, 2015 10:19
spray coffee payment web-service somewhat more complicated example
//from https://github.com/ppurang/spray-example
//below retreive(id) always returns Option[Order]
trait CoffeePaymentService extends HttpService with LiftJsonSupport {
self: PersistenceCoffee =>
override implicit def liftJsonFormats: Formats = Order.format
val paymentRoute =
pathPrefix("payment"/"order") {
@ppurang
ppurang / simplesprayhttpservice.scala
Created May 15, 2013 11:08
Simple HttpService using spray with a marshaller
trait Entry extends HttpService {
val entry = path("") {
get {
complete {
Link("order", "http://localhost:8080/order", "*/*,plain/text,application/json")
}
}
}
}
@ppurang
ppurang / scala meetup: May at Nokia
Last active December 17, 2015 02:59
scala meetup: May at Nokia
May is almost upon us and the series of great meetups needs to continue. This time around we are hosted by Nokia at their fairly new building. Being an ex-Nokian (hmmm did I get that right?), I am very curious how things are in the new building.
Here are the talks for the evening:
Christoph Knabe presents "Redundant History of Programming (~40 mins)
Piyush Purang presents "Spray It!" (~25 mins)
And we will start a discussion on the format of the meetups.
@ppurang
ppurang / isNotSameDayAndStillBefore
Created December 17, 2012 12:43
isNotSameDayAndStillBefore ..
import java.util.{Calendar => C}
import C._
def isSameDay(one: C, two: C) = one.get(ERA) == two.get(ERA) && one.get(YEAR) == two.get(YEAR) && one.get(DAY_OF_YEAR) == two.get(DAY_OF_YEAR)
//is the following the same?
def isSameDay2(one: C, two: C) = Math.abs(one.getTimeInMillis - two.getTimeInMillis) < 86400000
def isNotSameDayAndStillBefore(one: C, two: C) = !isSameDay(one, two) && one.before(two)
def breakIf[T: ClassTag](assertion: => Boolean, args: NamedParam*): Unit =
if (assertion) break[T](args.toList)
// start a repl, binding supplied args
def break[T: ClassTag](args: List[NamedParam]): Unit = savingContextLoader {
val msg = if (args.isEmpty) "" else " Binding " + args.size + " value%s.".format(
if (args.size == 1) "" else "s"
)
@ppurang
ppurang / AkkaKafkaMailboxTest.scala
Created October 4, 2012 09:54 — forked from stonegao/AkkaKafkaMailboxTest.scala
Akka 2.0 actors with Kafka backed durable mailboxes.
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.agent.Agent
import com.typesafe.config.ConfigFactory
import akka.event.Logging
import akka.actor.Props
import kafka.utils.Utils
import java.nio.ByteBuffer
@ppurang
ppurang / pr.md
Created August 20, 2012 21:24 — forked from piscisaureus/pr.md
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:

@ppurang
ppurang / gist:3385731
Created August 18, 2012 09:51 — forked from sadache/gist:3382059
Play2 new plugin: File NonBlocking/Async API - Copying a file
"copy file" in {
val fileGenerator = Enumerator.repeat(new java.util.Date.getTime.toString + "\n").through(Enumeratee.take(1000))
//val fileGenerator = Enumerator.repeat(new java.util.Date.getTime.toString + "\n") &>> Enumeratee.take(1000)
val f = FileChannel("/tmp/testwrite.txt").delete.writing.create
val f2 = FileChannel("/tmp/testwrite2.txt").delete.writing.create
fileGenerator // generates data
.through(RichEnumeratee.binarize()) // binarizes data to write into File