Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
SCALA TRAINING
the language matters
1
Agenda
• Function
• Implicit
• Monad
• Actor
Language features - functions
• high order function
3
Language - Implicit
• implicit convention
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))
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)
}
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] {//…}
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)
}
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
Language Features - Actor
• a small compute unit, including:
• behaviour
• state
• messaging
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
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
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

More Related Content

Scala the language matters

  • 3. Language features - functions • high order function 3
  • 4. Language - Implicit • implicit convention
  • 5. 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))
  • 6. 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) }
  • 7. 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] {//…}
  • 8. 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) }
  • 9. 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
  • 10. Language Features - Actor • a small compute unit, including: • behaviour • state • messaging
  • 11. 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
  • 12. 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
  • 13. 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