This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import java.util.{Date} | |
import play.db.sql._ | |
import play.db.sql.SqlParser._ | |
// User | |
case class User(id: Pk[Long], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@(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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |