NESCALA NOTES

by Hung Lin @hunglin

nescala 2013

Function Patterns for the Asynchronous Web

by Josh Suereth @ TypeSafe, author of Scala in Depth

The new Web

  • Asynchronous
  • Event driven
  • Data driven
  • Big data
  • Single page design
  • Composition of services
  • User experiences

Future


future {
  // Do sometime later
  ...
} map { result =>
  // Do something with result
  ...
}
						

Monadic Flow


def render(data: DbResult): Future[Html] = future(Html(...))
def handleRequest(...): Unit = {
  for {
    data <- query(...)
    html <- render(data)
  } yield writeHtml(socket, html)
}
						

Join


val users: Future[DbResult] = 
  query(...)
val projects: Future[DbResult] = 
  query(...)
val data: Future[(DbResult, DbResult)] = 
  (users zip projects)
						

List[Future[DbResult]] -> Future[List[DbResult]]
						

The Essence of the Iterator Pattern

Jeremy Gibbons and Bruno C. d. S. Oliveira @ Oxford University Computing Laboratory

  • Monadic Map
  • Applicative Functors
  • Monadic Applicative Functors
  • ...

Cake Pattern

by Daniel Spiewak, Code Commit

Not only for Dependency Injection


trait UserRepositoryComponent {
  val userRepository: UserRepository

  class UserRepository {
    ...
  }
} 

trait UserServiceComponent { 
  this: UserRepositoryComponent => 

  val userService: UserService  

  class UserService {
    ... 
  }
}						

Traits are modules


trait UserModule {
  def loadUser(id: Long): User
}
trait TweetModule {
  def post(userId: Long, body: String)
}
trait MySQLUserModule extends UserModule { ... }
trait TwitterModule extends TweetModule with UserModule { ... }
val universe = new MySQLUserModule
    with TwitterModule {}
						

Package is just a namespace to avoid name collision. So, instead import, use extends/with

Explicit dependencies

Interesting problem #1


trait A {
  val foo: String
}
trait B extends A {
  val bar = foo + "World"
}
class C extends B {
  val foo = "Hello"
  println(bar)
}
						

solution: use def or lazy val

Interesting problem #2


trait A {
  object stuff {
    object Foo
    object Bar
    object Baz
} }
trait B extends A {
  object moar {
    object Foo
    object Baz
    object Bar
} }
						

solution: use val

abstract override


  trait TraitA {
  	def method1(l: List[String]): List[String]
  }
  
  trait TraitB extends TraitA {
  	override def method1(l: List[String]) = l :+ "traitB"
  }

  trait TraitC extends TraitA {
  	abstract override def method1(l: List[String]) = super.method1(l) :+ "traitC"
  }
  
  class ClassA {
  	this: TraitA =>
  		def show() { println(method1(List("ClassA"))) }
  }
  
  (new ClassA with TraitB with TraitC).show()     //> List(ClassA, traitB, traitC)
						

Advanced Stream Processing

Programming + Math

Finagle

scala.collection.approximate

Play! 2 Framework

Dependency management

Day 2: Unconference

  • typeclass
  • unapply
  • ScalaTest
  • and a lot more

precog

  • 8 people attended
  • gave 5 talks

Audax @ nescala 2014

  • more people attend
  • give talks (Lift, ScalaTest, ...)
  • projects @ unconference