Skip to content

Instantly share code, notes, and snippets.

View sadache's full-sized avatar

Sadek Drobi sadache

View GitHub Profile
//in the scala interpreter I write
scala.actors.Futures.future(1).inputChannel.react({case _ => println(1) })
//and I get
java.lang.ClassCastException: java.lang.Thread cannot be cast to scala.concurrent.forkjoin.ForkJoinWorkerThread
at scala.concurrent.forkjoin.ForkJoinTask.fork(ForkJoinTask.java:481)
at scala.actors.scheduler.ForkJoinScheduler.executeFromActor(ForkJoinScheduler.scala:121)
at scala.actors.scheduler.DelegatingScheduler$class.executeFromActor(DelegatingScheduler.scala:42)
at scala.actors.scheduler.DaemonScheduler$.executeFromActor(DaemonScheduler.scala:17)
at scala.actors.Actor$class.scheduleActor(Actor.scala:628)
at scala.actors.ActorProxy.scheduleActor(ActorProxy.scala:20)
def substract(i:Int)=extension{
//owner represents the object you are extending
owner:Int =>
{
owner-i
}
}
//operator tild ~ instead of . for extensions
1~substract(2)~substract(1)~substract(1)
def fold[S,A](seed: =>S)(f:(Function0[S],A)=>S,stream:Stream[A]):S={
stream match{
case Stream.#::(x,xs) => f(()=>(fold[S,A](seed)(f,xs)),x)
case Stream.Empty =>seed}
}
fold[Stream[Int],Int](Stream.Empty)((s,a)=>Stream.cons(a,s()),Stream.fill(10000)({println("oooo ");10000}))
outputs
@sadache
sadache / NPE.scala
Created October 20, 2010 07:57
Simple example of the Option type in Scala
object CityDictionary{
def getMeACity(cityCode:String) :Option[City]={
if(cityCode=="75") Some(City("Paris","75"))
else None
}
case class City(name: String,code:String)
}
object NPEApplication {
private val postWithAuthor = Post ~< User
def allWithAuthor : List[Post ~ User] =
SQL("""select * from Post p
join User u on p.author_id = u.id """)
.as(postWithAuthor*)
def byIdWithAuthorAndComments(id: Long) :
Option[Post ~ User ~ List[Comment]] = {
package models
import java.util.{Date}
import play.db.sql._
import play.db.sql.SqlParser._
// User
case class User(id: Pk[Long],
def prevNext = {
SQL(
"""
(
select *, 'next' as pos from post
where postedAt < {date} order by postedAt desc limit 1
)
union
(
select *, 'prev' as pos from post
@sadache
sadache / demo.scala
Created October 18, 2011 14:23 — forked from nicmarti/demo.scala
Yes I am proud to be able to write this kind of Code
@(contacts:List[(Long,String,String)])
<html>
<head>
<title>ZenContact - Play! 2 Sample application</title>
<link rel="shortcut icon" type="image/png" href="http://www.playframework.org/public/images/favicon.png">
<link rel="stylesheet" type="text/css" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Varela+Round|Droid+Sans">
</head>
<body>
@sadache
sadache / code.scala
Created December 12, 2011 09:27 — forked from gre/code.scala
Play20 Promise sequence function
// What is a Promise sequence function ?
// A function which transform a List[Promise[A]] into a Promise[List[A]]
// First naive implementation. It's a synchronous implementation (blocking).
def sequencePromises[A](list: List[Promise[A]]): Promise[List[A]] =
list.foldLeft(Promise.pure(Promise[List[A]]()))((s,p) => s.flatMap( s => p.map(a => s :+ a)))
//should be in the lines of this, didn't try to typecheck it though.
@sadache
sadache / ServerEventsComparison.scala
Created April 29, 2012 17:22 — forked from fcroiseaux/ServerEventsComparison.scala
Comparison of Comet, SSE and WebSocket server to client communication with Playframework 2.0 in Scala
/**
* Handles the comet event stream.
*/
def cometStream = Action {
AsyncResult {
implicit val timeout = Timeout(5.seconds)
val actor=Akka.system.actorOf(Props[EventListener])
// Actor is listening for event on the eventStream
Akka.system.eventStream.subscribe(actor,classOf[ChangeEvent])
// For each event, stream the data to client