scala the language matters

13
SCALA TRAINING the language matters 1

Upload: xiaojun-ren

Post on 13-Jul-2015

67 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Scala the language matters

SCALA TRAININGthe language matters

1

Page 2: Scala the language matters

Agenda

• Function

• Implicit

• Monad

• Actor

Page 3: Scala the language matters

Language features - functions

• high order function

3

Page 4: Scala the language matters

Language - Implicit

• implicit convention

Page 5: Scala the language matters

Language - Implicit

val personJson = """{ "name": "Jason" }”""

val r1 = httpClient(Request( GET, new URI("http://api.rest.org/person/"), Map(), None))

val r2 = httpClient(Request( POST, new URI("http://api.rest.org/person/"), Map(), Some(personJson)))

val id = r2.headers(“X-Person-Id").head

val r3 = httpClient(Request( GET, new URI("http://api.rest.org/person/" + id), Map(), None))

val r4 = httpClient(Request( GET, new URI("http://api.rest.org/person/"), Map(), None))

val r5 = httpClient(Request( DELETE, new URI("http://api.rest.org/person/" + id), Map(), None))

Page 6: Scala the language matters

Language - Implicit

using(_ url "http://api.rest.org") { implicit rb =>

GET / "person" asserting (StatusCode === Status.OK, BodyAsPersonList === EmptyList)

val id = POST / "person" body personJson

asserting (StatusCode === Status.Created) returning (header(“X-Person-Id"))

GET / "person" / id asserting (StatusCode === Status.OK, BodyAsPerson === Jason)

GET / "person" asserting (StatusCode === Status.OK, BodyAsPersonList === Seq(Jason))

DELETE / "person" / id asserting (StatusCode === Status.OK)

GET / "person" asserting (StatusCode === Status.OK, BodyAsPersonList === EmptyList)

}

Page 7: Scala the language matters

Language Feature - Monad

sealed abstract class Try[+T] case class Success[+T](value: T) case class Failure[+T](exception: Throwable)

case abstract class Option[T] case class Some[T] case object None extends Option[Nothing] {//…}

Page 8: Scala the language matters

Language Feature - Monad

//in User def findById(id: Long):Try[User] = { //Success(user) or Failure(e) }

//in Post def findByUser(user: User):Try[List[Post]] = { //Success(posts)or Failure(e) }

val posts: Try[List[Post]] = User.findById(userId).map { user => Post.findByUser(user) }

posts match { case Success(p) => render(p) case Failure(e) => error(e) }

Page 9: Scala the language matters

Language features - Monad

import Math.abs

type Birds = Int type Pole = (Birds, Birds)

def landLeft(n: Int, p: Pole):Option[Pole] = p match { case (left, right) if abs(left + n - right) < 4 => Some(left + n, right) case _ => None }

def landRight(n: Int, p: Pole) = p match { case (left, right) if abs(left - n - right) < 4 => Some(left , right + n) case _ => None }

val result = landLeft(1, (0, 0)). flatMap{ landLeft(2,_:Pole) }. flatMap{ landRight(5, _:Pole) }

println(result)

9

Page 10: Scala the language matters

Language Features - Actor

• a small compute unit, including:

• behaviour

• state

• messaging

Page 11: Scala the language matters

Language Features - Actor

• rules of when a actor received a message:

• create a new actor

• send message to a new actor

• define behaviour when next message arrived

• the most important feature

• a actor is always thread safe

• concurrent is multiple actor’s behaviour

Page 12: Scala the language matters

Language Features - Actor

class MyActor extends Actor { def receive = { case MsgType1 => //do something case MsgType2 => //do something else } }

val actorRef = system.actorOf[Props[MyActor]] actorRef ! MsgType1

Page 13: Scala the language matters

References

• http://twitter.github.io/scala_school/zh_cn/index.html

• http://twitter.github.io/effectivescala/index-cn.html

• http://typesafe.com/blog/all

• http://www.cakesolutions.net/teamblogs

• http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

13