Demystifying functional programming with Scala
-
Upload
denis -
Category
Engineering
-
view
243 -
download
3
Transcript of Demystifying functional programming with Scala
Scalafunctional programming demystified
@_denisftw_http://appliedscala.com
UsersTwitter (Ruby)Foursquare (Java/.NET)Tumblr (PHP)Guardian (Java)
Klout (PHP)Coursera (PHP)LinkedIn* Netflix* (JVM)
History
2003 - Released in EPFL2008 - ‘Programming in Scala’2010 - Version 2.82011 - Typesafe Inc. founded2012 - First course on Coursera2013 - Version 2.10
Recipe
● Take Java-like syntax● Remove specifics and
unnecessary limitations● Mix in functional features*
Complexity
● About 40 reserved words● Only 2 namespaces vs. 4 in Java (fields, methods,
packages, types)● Methods instead of operators● No primitives (AnyVal)
Hierarchy
scala
Doublescala
Unit
scala
AnyVal
scala
Any
scala
AnyRef
java.lang
Stringscala.collection.immutable
List
scala
Null
scala
Nothing
scala
Int
Collections
s.c
Seqs.c
Set
scala.collection
Iterable
scala.collection
Traversable
s.c
Map
s.c
Lists.c
Buffers.c
IndexedSeq
● Redesigned in 2.8● 100500+ methods● Immutable by default● Easy conversions● Buffers
Classes
class User (var id: Int, val name: String) {
override def toString = {
id + ". " + name
}
}
scala> new User(1, "Simon")
Objects
object User {
def apply(id: Int, name: String) =
new User(id, name)
}
scala> User.apply(2, "Bob")
Objects
object User {
def apply(id: Int, name: String) =
new User(id, name)
}
scala> User.apply(2, "Bob")
scala> User(3, "Chris")
Functional features● Functional data types● Tail recursion● Immutability● Higher-order functions● Currying● Pattern matching● Lazy evaluation
Functional data typesval l = 1 :: 2 :: 3 :: Nil
// List(1, 2, 3)
l.head // 1
l.tail // List(2, 3)
l.isEmpty // false s.c.i
Nils.c.i
::[T]
scala.collection.immutable
List[+T]
Imperative example
def concat(list: List[Any]): String = {
val iter = list.iterator
var result = ""
while (iter.hasNext) {
result += iter.next
}
result
}
Tail recursion
def concatT(s: String, list: List[Any]): String =
if (list.isEmpty)
s
else
concatT(s + list.head, list.tail)
Tail recursiondef concat(l: List[Any]): String = {
@tailrec
def concatT(s: String, list: List[Any]): String =
if (list.isEmpty)
s
else
concatT(s + list.head, list.tail)
concatT("", l)
}
Pattern matching
def concat(l: List[Any]): String =
l match {
case Nil => ""
case head :: tail =>
head + concat(tail)
}
flatMapval l1 = List(1, 2, 3)
val l2 = List("a", "b")
l1.flatMap{ el1 =>
l2.map{ el2 => el1 + el2 }
}
// 1a, 1b, 2a, 2b, 3a, 3b
for expressionsl1.flatMap { el1 =>
l2.map { el2 => el1 + el2 }
}
for {
el1 <- l1
el2 <- l2
} yield el1 + el2
Stream
val numbers = Stream.from(0)
scala.collection.immutable
Stream[+T]
scala.collection
Traversable[+T]
Option
isDefined: Boolean
get: T
getOrElse[T](default: T): T
scala
Nonescala
Some[+T]
scala
Option[+T]
Option
maybeUser.map { user =>
user.name
}.map { name =>
"Welcome back," + name
}.getOrElse("Hi")
Option[User]
Option[String]
Option[String]
String
map
map
getOrElse
Trydef parse(js: Js): String
val nameT = Try {
parse(js)
}
val maybeName = nameT.toOption
scala.util
Failure[+T]scala.util
Success[+T]
scala.util
Try[+T]
Trydef parse(js: Js): String
def store(name: String): Int
for {
name <- Try(parse(json))
id <- Try(store(name))
} yield id
scala.util
Failure[+T]scala.util
Success[+T]
scala.util
Try[+T]
Futuredef query(q: String): List[String]
val resF = Future { query(q) }
resF.onSuccess {
case list => …
}
resF.onFailure {
case ex => …
}
scala.concurrent
Future[+T]
Futuredef query(q: String): List[String]
def store(l: List[String]): Int
queryF.onSuccess {
case list => {
val storeF = Future { store(list) }
storeF.onSuccess { case id => … }
}
}
scala.concurrent
Future[+T]
Futureval resultsF = Future{ query("WTF") }
for {
results <- resultsF
id <- Future{ store(results) }
} yield id
scala.concurrent
Future[+T]
Wait... there’s more!
● object-oriented programming● arcane things● actors● ecosystem● tools and frameworks
The book● Suited for people with no prior
experience in Scala● Includes functional programming tutorial● Focuses on Play 2.5● ScalikeJDBC, MacWire, Akka● Webpack, Sass, React, EcmaScript 6