Skip to content

Instantly share code, notes, and snippets.

@aztek
aztek / FRP.scala
Last active December 16, 2015 03:59
Trivial FRP for Scala with scala-idioms
import idioms._ // http://github.com/aztek/scala-idioms
trait Cell[T] {
def ! : T
def := (value: T) { throw new UnsupportedOperationException }
}
val frp = new Idiom[Cell] {
def pure[A](a: ⇒ A) = new Cell[A] {
private var value = a
@sadache
sadache / gist:3316242
Created August 10, 2012 18:09
Http Chunked protocole implemented on top of Play's API
Enumeratee.map[Array[Byte]]{ e =>
val length = e.size
val CRLF = "\r\n".getBytes("UTF-8")
(length.toHexString.getBytes ++ CRLF ++ e ++ CRLF):Array[Byte]
} ><>
Enumeratee.trailing(Seq((0.toHexString.getBytes) ++ (CRLF ++ CRLF)))
@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)