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

PDF
Scala: Functioneel programmeren in een object georiënteerde wereld
PDF
Advanced Tagless Final - Saying Farewell to Free
PDF
Ruslan.shevchenko: most functional-day-kiev 2014
PDF
Scalaエンジニアのためのモナド入門
PDF
The Death of Final Tagless
PDF
Scala Hands On!!
PDF
Делаем пользовательское Api на базе Shapeless
PDF
openCypher Technology Compatibility Kit (TCK)
Scala: Functioneel programmeren in een object georiënteerde wereld
Advanced Tagless Final - Saying Farewell to Free
Ruslan.shevchenko: most functional-day-kiev 2014
Scalaエンジニアのためのモナド入門
The Death of Final Tagless
Scala Hands On!!
Делаем пользовательское Api на базе Shapeless
openCypher Technology Compatibility Kit (TCK)

What's hot (11)

PDF
Post-Free: Life After Free Monads
PDF
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
PDF
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
PPTX
Visualization team presentation
PDF
Scalaz 8: A Whole New Game
PDF
Julio Capote, Twitter
PDF
Principled Error Handling with FP
PDF
Building a Functional Stream in Scala
PPTX
Guava - Elements of Functional Programming
PDF
Ozma: Extending Scala with Oz concurrency
PDF
Refactoring Functional Type Classes
Post-Free: Life After Free Monads
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Visualization team presentation
Scalaz 8: A Whole New Game
Julio Capote, Twitter
Principled Error Handling with FP
Building a Functional Stream in Scala
Guava - Elements of Functional Programming
Ozma: Extending Scala with Oz concurrency
Refactoring Functional Type Classes
Ad

Viewers also liked (11)

PDF
Why functional why scala
PDF
Monad presentation scala as a category
PPTX
Introduction to Monads in Scala (2)
PDF
Introduction to Option monad in Scala
PDF
Thinking functional-in-scala
PPTX
Introduction to Monads in Scala (1)
PDF
Developers Summit 2015 - Scala Monad
PPTX
Advanced Functional Programming in Scala
KEY
Scala: functional programming for the imperative mind
PPTX
Functional Programming Fundamentals
PDF
Introduction to Functional Programming with Scala
Why functional why scala
Monad presentation scala as a category
Introduction to Monads in Scala (2)
Introduction to Option monad in Scala
Thinking functional-in-scala
Introduction to Monads in Scala (1)
Developers Summit 2015 - Scala Monad
Advanced Functional Programming in Scala
Scala: functional programming for the imperative mind
Functional Programming Fundamentals
Introduction to Functional Programming with Scala
Ad

Similar to Scala the language matters (20)

PPTX
An introduction to scala
PDF
Introduction to scala
PPTX
Introduction to kotlin + spring boot demo
PDF
Programming in Scala - Lecture Four
PDF
Reactive Software Systems
PDF
Scala Paradigms
PDF
Scala In The Wild
PDF
Quick introduction to scala
PPTX
The dark side of Akka and the remedy - bp.scala meetup
PDF
Short intro to scala and the play framework
PDF
Scala Implicits - Not to be feared
PDF
The Scala Programming Language
PDF
Meet scala
PDF
Introduction to programming in scala
PDF
GR8Conf 2011: GPars
PDF
Introducing Akka
PPTX
Intro to Scala
KEY
Introduction to Actor Model and Akka
PDF
Programming in scala - 1
PDF
Typesafe stack - Scala, Akka and Play
An introduction to scala
Introduction to scala
Introduction to kotlin + spring boot demo
Programming in Scala - Lecture Four
Reactive Software Systems
Scala Paradigms
Scala In The Wild
Quick introduction to scala
The dark side of Akka and the remedy - bp.scala meetup
Short intro to scala and the play framework
Scala Implicits - Not to be feared
The Scala Programming Language
Meet scala
Introduction to programming in scala
GR8Conf 2011: GPars
Introducing Akka
Intro to Scala
Introduction to Actor Model and Akka
Programming in scala - 1
Typesafe stack - Scala, Akka and Play

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
Teaching material agriculture food technology
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Cloud computing and distributed systems.
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Empathic Computing: Creating Shared Understanding
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Teaching material agriculture food technology
Understanding_Digital_Forensics_Presentation.pptx
Spectroscopy.pptx food analysis technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Unlocking AI with Model Context Protocol (MCP)
Diabetes mellitus diagnosis method based random forest with bat algorithm
Review of recent advances in non-invasive hemoglobin estimation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Cloud computing and distributed systems.
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

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