Skip to content

Instantly share code, notes, and snippets.

View sadache's full-sized avatar

Sadek Drobi sadache

View GitHub Profile
package models
import java.util.{Date}
import play.db.sql._
import play.db.sql.SqlParser._
// User
case class User(id: Pk[Long],
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]] = {
@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 {
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
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)
//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)
case class Error[+E,+A](e:Either[E,A]) {
def flatMap[B,EE>:E](f:A => Error[EE,B]):Error[EE,B] ={
Error(e.right.flatMap(a=> f(a).e))
}
def map[B](f:A=>B):Error[E,B]={
Error(e.right.map(f))
}
def toOptionLoggingError():Option[A]={
e.left.map(m => {error(m.toString); m}).right.toOption
}
// I really do not like using the syntax that removes points
scala> List("one","two") foldLeft ("") (_+_)
<console>:6: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
List("one","two") foldLeft ("") (_+_)
^
// Int ???
scala> List("one","two") foldLeft ("") ((_+_):(String,String) => String)
<console>:6: error: type mismatch;
case class Error(msg:String)
val one:Either[Int,Error]= Left(1)
val two:Either[Int,Error]= Right(Error("oops"))
val result=for(a<-one.left; t <- two.left) yield a + t
// defined class Error
// one: Either[Int,Error] = Left(1)
// two: Either[Int,Error] = Right(Error(oops))
// result: Either[Int,Error] = Right(Error(oops))
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