Skip to content

Instantly share code, notes, and snippets.

View sadache's full-sized avatar

Sadek Drobi sadache

View GitHub Profile
trait Option[+A] {
@inline final def cata[Z](some: A => Z, none: => Z) = this match {
case Some(a) => some(a)
case None => none
}
}
object Option {
private[Option] case class Some[+A](a: A) extends Option[A]
private[Option] case object None extends Option[Nothing]
@sadache
sadache / AA.md
Created July 8, 2012 21:29
Is socket.push(bytes) all you need to program Realtime Web apps?

Is socket.push(bytes) all you need to program Realtime Web apps?

One of the goals of Play2 architecture is to provide a programming model for what is called Realtime Web Applications.

Realtime Web Applications

Realtime Web Applications are applications that make use of Websockets, Server Sent Events, Comet or other protocols offering/simulating an open socket between the browser and the server for continuous communication. Basically, these applications let users work with information as it is published - without having to periodically ping the service.

There are quite a few web frameworks that target the development of this type of application: but usually the solution is to simply provide an API that allows developers to push/receive messages from/to an open channel, something like:

@sadache
sadache / gist:3026886
Created July 1, 2012 05:01
Bits'bout Play2 Architecture

Bits'bout Play2 Architecture

Play2 Simple HTTP API

Essential blueprint of Play2 architecture is pretty simple and it should be easy to explain in a fairly short blog post. The framework can be understood progressively at different levels; each time having better exposure to some aspects of its design.

The core of Play2 is really small, surrounded by a fair amount of useful APIs, services and structure to make Web Programming tasks easier.

Basically, Play2 is an API that abstractly have the folllowing type

@sadache
sadache / gist:2939230
Created June 15, 2012 23:37
Parsing progressively a csv like file with Play2 and Iteratees

If your csv doesn't contain escaped newlines then it is pretty easy to do a progressive parsing without putting the whole file into memory. The iteratee library comes with a method search inside play.api.libs.iteratee.Parsing :

def search (needle: Array[Byte]): Enumeratee[Array[Byte], MatchInfo[Array[Byte]]]

which will partition your stream into Matched[Array[Byte]] and Unmatched[Array[Byte]]

Then you can combine a first iteratee that takes a header and another that will fold into the umatched results. This should look like the following code:

// break at each match and concat unmatches and drop the last received element (the match)
@jto
jto / funky.scala
Created April 23, 2012 21:49
Funky enumerator usage
package controllers
import play.api._
import play.api.mvc._
import play.api.libs.ws._
import play.api.libs.iteratee._
import play.api.libs.concurrent._
object Application extends Controller {
module Main where
class Monad64 a ma mb | ma mb -> a where
(>>==) :: ma -> ( a -> mb) -> mb
data Maybe64 a = Just64 a
|Nothing64
deriving Show
instance Monad64 a (Maybe64 a) (Maybe64 b) where