scala for the doubters
TRANSCRIPT
![Page 1: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/1.jpg)
Scala for the doubtersMax Klyga
@Neku42
![Page 2: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/2.jpg)
–James Gosling
“If I were to pick a language to use today other than Java,
it would be Scala.”
![Page 3: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/3.jpg)
Scala
![Page 4: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/4.jpg)
–James Iry, “A Brief, Incomplete, and Mostly Wrong History of Programming Languages”
“A drunken Martin Odersky sees a Reese's Peanut Butter Cup ad featuring
somebody's peanut butter getting on somebody else's chocolate and has an idea. He creates Scala, a language that
unifies constructs from both object oriented and functional languages. This
pisses off both groups and each promptly declares jihad.”
![Page 5: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/5.jpg)
Scala
OOP FP
![Page 6: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/6.jpg)
![Page 7: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/7.jpg)
Lightweight syntax
object Main extends Application {
val phonebook = Map(
"Alice" -> "212-34-56",
"Bob" -> "265-43-21"
)
phonebook += ("Carl" -> "298-76-54")
println(phonebook("Alice"))
for (i <- 1 to 10) {
println(i)
}
}
Scala
![Page 8: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/8.jpg)
Lightweight syntax
public class Person
{
private String firstName;
private String lastName;
String getFirstName() { return firstName; }
void setFirstName(String firstName) { this.firstName = firstName; }
String getLastName() { return lastName; }
void setLastName(String lastName) { this.lastName = lastName; }
int hashCode() { .... }
boolean equals(Object o) { .... }
}
Java
![Page 9: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/9.jpg)
Lightweight syntax
case class Person(var firstName:String, var lastName:String)
Scala
![Page 10: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/10.jpg)
Lightweight syntax
boolean hasUpperCase = false;
for (int i = 0; i < name.length(); ++i) {
if (Character.isUpperCase(name.charAt(i))) {
hasUpperCase = true;
break;
}
}
Java
![Page 11: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/11.jpg)
Lightweight syntax
val hasUpperCase = name.exists(_.isUpperCase)
Scala
![Page 12: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/12.jpg)
Lightweight syntax
Predicate<Character> isUpperCase = new Predicate<Character>() {
public Character apply(Character ch) {
return Character.isUpperCase(ch);
}
};
boolean hasUpperCase = Iterables.any(Lists.charactersOf(name), isUpperCase);
Java + Guava
![Page 13: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/13.jpg)
Lightweight syntax
Predicate<Character> isUpperCase = (ch) -> Character.isUpperCase(ch);
boolean hasUpperCase = Iterables.any(Lists.charactersOf(name), isUpperCase);
Java 8 + Guava
![Page 14: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/14.jpg)
Lightweight syntax
boolean hasUpperCase = name.chars().anyMatch((ch) -> Character.isUpperCase(ch));
Java 8
![Page 15: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/15.jpg)
Lightweight syntax
boolean hasUpperCase = name.chars().anyMatch((ch) -> Character.isUpperCase(ch));
Java 8
Scala
val hasUpperCase = name.exists(_.isUpperCase)
![Page 16: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/16.jpg)
LambdasJava 8
•Can only access final variables•Cannot mutate variables in outer scope
Scala
•Can access any variables•Can mutate any accessible variables
![Page 17: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/17.jpg)
TraitsJava 8
•Can define default methods in interfaces(Java 9 allows private methods)
•“Diamond” inheritance is forbidden
Scala
•No restrictions on method implementation•Method invocation chain determined by inheritance order
![Page 18: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/18.jpg)
Composable
Everything is an expression
![Page 19: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/19.jpg)
Composable
var x:Int
if (…) {
x = 10
} else {
x = 0
}
![Page 20: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/20.jpg)
Composable
val x = if (…) {
10
} else {
0
}
![Page 21: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/21.jpg)
Composable
var x = List[Int]()
for (i <- 1 to 10) {
x = i :: x
}
![Page 22: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/22.jpg)
Composable
var x = for (i <- 1 to 10) {
yield i
}
![Page 23: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/23.jpg)
Composable
Everything is an expression
if for while try match
![Page 24: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/24.jpg)
Composable
Things nest
![Page 25: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/25.jpg)
Composableimport com.dull.lib
class Eggs {
import org.cool.lib
def bacon {
import org.other.cool.lib
…
}
}
![Page 26: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/26.jpg)
Composableclass Eggs {
def bacon {
def inner(…) {
def innerer {
…
}
…
}
…
}
}
![Page 27: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/27.jpg)
Pattern matchingsealed abstract class ChatProtocol
case class Message(userId:Int, text:String) extends ChatProtocol
case class Join(userId:Int, name:String) extends ChatProtocol
case class Leave(userId:Int) extends ChatProtocol
command match {
case Message(id, text) => println(s"${users(id)}: ${text}")
case Join(id, name) => {
users += (id, name)
println(s"${name} joined")
}
case Leave(id) => {
println(s"${users(id)} left")
users -= id
}
}
![Page 28: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/28.jpg)
REPLJava 9 will finally get it too
![Page 29: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/29.jpg)
So much more• Everything is an object
• Limited type inference
• Operator overloading (Everything is a method)
• Algebraic data types
• Partial functions
• Tuples
• Value classes
![Page 30: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/30.jpg)
![Page 31: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/31.jpg)
–Alan Perlis
“Syntactic sugar causes cancer of the semi-colons.”
![Page 32: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/32.jpg)
![Page 33: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/33.jpg)
![Page 34: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/34.jpg)
![Page 35: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/35.jpg)
Implicits
• Conversions
• Extension methods via Rich Interface Pattern
• Remove boilerplate code
![Page 36: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/36.jpg)
Implicits
• Conversions
• Extension methods via Rich Interface Pattern
• Remove boilerplate code
![Page 37: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/37.jpg)
Generic code
• Partial application
• Variance annotations
• Implicits
• Macros
![Page 38: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/38.jpg)
With great power comes great responsibility
![Page 39: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/39.jpg)
Sleep of reason produces monsters
![Page 40: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/40.jpg)
Operator overloading abuse
val intermediateHandler = (:/(host, port) / target <<? params >:> identity)
https://github.com/dispatch/reboot
def root = jsonObj | jsonArray
def jsonObj = "{" ~> repsep(objEntry, ",") <~ "}" ^^ { case vals:List[_] => JSONObject(Map(vals:_*)) }
def jsonArray = "[" ~> repsep(value, ",") <~ "]" ^^ { case vals:List[_] => JSONArray(vals) }
def objEntry = stringVal ~ (":" ~> value) ^^ { case x ~ y => (x, y) }
https://github.com/scala/scala-parser-combinators
Unicode operators in ScalaZ: ≠ ∋ ⇏ ★ ∅
![Page 41: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/41.jpg)
Learning Scala will make you a better Java programmer
![Page 42: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/42.jpg)
Learning Scala will make you a better Java programmer
• Data transformation pipeline instead of CRUD
• “Sane” error handling, data validation
http://fsharpforfunandprofit.com/rop/
• Computation and data composition
For comprehensions (Monads …)
![Page 43: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/43.jpg)
–Paul Graham
“you could get smarter programmers to work on a Python project than you could to
work on a Java project.”
![Page 44: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/44.jpg)
The Python Paradox
![Page 45: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/45.jpg)
The Scala Paradox
![Page 46: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/46.jpg)
Why use Scala?
• eDSL
• Parsers
• Distributed computing infrastructure
![Page 47: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/47.jpg)
eDSLval orders = List[Order](
// use the default pricing strategy
new Order to buy(200 sharesOf "GOOGLE")
maxUnitPrice 300
using defaultPricing,
// use a custom pricing strategy
new Order to sell(200 bondsOf "Sun")
maxUnitPrice 300
using {
(qty, unit) => qty * unit - 500
}
)
http://debasishg.blogspot.com/2008/05/designing-internal-dsls-in-scala.html
![Page 48: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/48.jpg)
eDSLclass HelloWorldSpec extends Specification { def is = s2"""
This is a specification for the 'Hello world' string
The 'Hello world' string should
contain 11 characters $e1
start with 'Hello' $e2
end with 'world' $e3
"""
def e1 = "Hello world" must haveSize(11)
def e2 = "Hello world" must startWith("Hello")
def e3 = "Hello world" must endWith("world")
}
https://etorreborre.github.io/specs2/
![Page 49: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/49.jpg)
eDSLobject SquareRoot extends Baysick {
def main(args:Array[String]) = {
10 PRINT "Enter a number"
20 INPUT 'n
30 PRINT "Square root of " % "'n is " % SQRT('n)
40 END
RUN
}
}
https://github.com/fogus/baysick
![Page 50: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/50.jpg)
![Page 51: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/51.jpg)
Java ❤️ Scala
![Page 52: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/52.jpg)
Java ❤️ Scala
• Scala Maven plugin - http://davidb.github.io/scala-maven-plugin/
![Page 53: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/53.jpg)
Learn more• Twitter Scala school
http://twitter.github.io/scala_school/
• Scala for the Impatient
http://www.horstmann.com/scala/
• Programming in Scala
http://www.artima.com/shop/programming_in_scala_2ed
• Functional Programming in Scala
http://manning.com/bjarnason/
![Page 54: Scala for the doubters](https://reader031.vdocument.in/reader031/viewer/2022032305/55d0722fbb61eb662e8b469b/html5/thumbnails/54.jpg)
Questions
Max Klyga@Neku42