An introductory speech about Scala I held at Pordenone during AppDays2015. In this talk I highlighted some basic features of Scala (compact syntax, objects, case classes, pattern matching, implicit conversion). There are some pointers to www.cosenonjaviste.it, an italian blog about programming
7. WELL..SOMETHING HAPPENED IN LAST 10~15
YEARS
Cores 1 → 2 → 4 → ...
Server nodes 1 → 10's → 1000’s.
Response times seconds → milliseconds
Maintenance downtimes hours → nones
Data volum GBs → TBs and more
8. THE "RISE" OF THE FUNCTIONAL
PROGRAMMING (FP)
functions are used as the fundamental building blocks of
a program
avoids changing-state and mutable data
calling a function f twice with the same value for an
argument x will produce the same result f(x)
easy scaling in and out
many deep interesting concepts that don't fit a life.. ehm..
slide ;)
10. A BRIEF, INCOMPLETE, AND MOSTLY WRONG
HISTORY OF PROGRAMMING LANGUAGES
2003 - 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.
11. SCALA - SCALABLE LANGUAGE
blends Object Oriented and Functional Programming
powerful static type system (but feels dynamic)
concise and expressive syntax
compiles to Java byte code and runs on the JVM
interoperable with Java itself (both ways!)
12. ANOTHER HIPSTER STUFF?
Twitter, LinkedIn, FourSquare, NASA, ..
Functional programming in Scala & Principles of Reactive
Programming @ Coursera
400K online students
http://www.quora.com/What-startups-or-tech-
companies-are-using-Scala
13. ANOTHER *USA* HIPSTER STUFF?
Lambdacon (Bologna) ~ 280 dev
Scala Italy (Milano) ~ 150 + 70
Scala Meetup (Milano) ~ 50
G+ Community ~ 80
15. SOME FEATURE WE'LL SEE/1
every value is an object
functions are "just" objects
compiler infer types (static typed)
(almost) everything is an expression
every operation is a method call
16. SOME FEATURE WE'LL SEE/2
language is designed to grow
collections
traits
easy singleton
every operation is a method call
17. CONCISE SYNTAX - JAVAPIZZA I
public class JavaPizza {
private Boolean tomato = true;
private Boolean cheese = true;
private Boolean ham = true;
private String size = "NORMAL";
public JavaPizza(Boolean tomato, Boolean cheese,
Boolean ham, String size) {
this.setTomato(tomato);
this.setCheese(cheese);
this.setHam(ham);
this.setSize(size);
}
18. CONCISE SYNTAX - JAVAPIZZA/II
public Boolean getTomato() {
return tomato;
}
public void setTomato(Boolean tomato) {
this.tomato = tomato;
}
public Boolean getCheese() {
return cheese;
}
public void setCheese(Boolean cheese) {
this.cheese = cheese;
}
19. CONCISE SYNTAX - JAVAPIZZA/III
public Boolean getHam() {
return ham;
}
public void setHam(Boolean ham) {
this.ham = ham;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
}
20. CONCISE SYNTAX - PIZZA
class Pizza(var tomato: Boolean = true,
var cheese: Boolean = true,
var ham: Boolean = true,
var size: String = "NORMAL")
everything is public by default, change it if needed
var => something you can change, val => something
final
specify defaults if needed
types are after parameters
setters and getters are generated
member variables are private (uniform access principle)
no semicolon needed
21. CREATING OBJECTS
val pizza = new Pizza(true, false, true, "HUGE") // type inference
is the same as
val pizza : Pizza = new Pizza(true, false, true, "HUGE")
22. VAL != VAR
var secondPizza = new Pizza(true, true, true, "NORMAL")
secondPizza = new Pizza(false, false, true, "HUGE") // will compile
val thirdPizza = new Pizza(true, false, true, "SMALL")
thirdPizza = secondPizza // won't compile
var: side effect, OOP programming
val: no side effect, functional programming
23. DEFINING METHODS
def slice(numberOfPieces: Int): Unit = {
println(s"Pizza is in ${numberOfPieces} pieces")
}
a method starts with a def
return type is optional (with a little exception)
Unit ~ void in Java
return keyword is optional, last expression will be
returned
plus: string interpolation
24. EXPRESSIVE SYNTAX
val pizza1 = new Pizza(true, true, false, "NORMAL")
val pizza2 = new Pizza(true, true)
val pizza3 = new Pizza(tomato = true, ham = true, size = "HUGE", cheese =
val pizza4 = new Pizza(size = "SMALL")
if parameters have default, you can omit them on right
you can pass parameter using names (without order)
you can pass only parameters you want using name, if
others have default
more on classes:
cosenonjaviste.it/creare-una-classe-in-scala/
25. SINGLETONS/I
object Oven {
def cook(pizza: Pizza): Unit = {
println(s"Pizza $pizza is cooking")
Thread.sleep(1000)
println("Pizza is ready")
}
}
a singleton uses the object keyworkd
pattern provided by the language
plus: a Java class used inside Scala
26. SINGLETONS/II
object Margherita extends Pizza(true, false, false, "NORMAL") {
override def toString = "Pizza Margherita"
}
Oven.cook(new Pizza())
println(Margherita.toString())
objects can extends classes
every overridden method must use override keyword
27. CASE CLASSES
case class Pizza(tomato: Boolean = true, cheese: Boolean = true, ham:
val p = Pizza(cheese = false)
// the same as val p = new Pizza(cheese = false)
syntactic sugar, but useful
every parameter is immutable
a companion object is created
factory, pattern matching
more on http://cosenonjaviste.it/scala-le-case-class/
28. INHRERITANCE/I
trait PizzaMaker {
def preparePizza(pizza: Pizza): String // this is abstract
}
trait Waiter {
def servePizza(pizza: Pizza) = println("this is your pizza")
}
abstract class Worker {
def greet = {
println("I'm ready to work")
}
}
object Pino extends Worker with PizzaMaker with Waiter {...}
29. INHRERITANCE/II
a class can inherite only one other class
trait ~ Java 8 interface
a class can mix-in many traits
trait linearization resolves the diamond problem (mostly)
31. PATTERN MATCHING/I
object Pino extends Worker with PizzaMaker {
def preparePizza(pizza: Pizza): String = {
pizza match {
case Pizza(false, cheese, ham, size) =>
"No tomato on this"
case Pizza(_, _, _, "SMALL") =>
"Little chamption, your pizza is coming"
case pizza@Margherita =>
s"I like your ${pizza.size} Margherita"
case _ => "OK"
}
}
}
32. PATTERN MATCHING/II
a super flexible Java switch (but very different in
nature)
in love with case classes, but possible on every class
can match partial definition of objects
can match types
_ is a wildcard
33. WORKING WITH COLLECTIONS AND
FUNCTIONS/I
val order = List(pizza1, pizza2, pizza3, pizza4, pizza5)
val noTomato: (Pizza => Boolean) = (p => p.tomato == false)
order
.filter(noTomato)
.filter(p => p.ham == true)
.map(pizza => Pino.preparePizza(pizza))
.map(println)
34. WORKING WITH COLLECTIONS AND
FUNCTIONS/II
easy to create a collection
functions are objects, you can pass them around
high order functions
you can use methods as functions
filter, map, fold and all the rest
35. OPERATORS ARE JUST METHODS (AN
EXAMPLE)
case class Pizza(tomato: Boolean = true, cheese: Boolean = true, ham: Boolean =
def slice(numberOfPieces: Int) =
println(s"Pizza is in ${numberOfPieces} pieces")
def /(numberOfPieces: Int) = slice(numberOfPieces)
}
val pizza = Pizza()
pizza.slice(4)
pizza./(4)
pizza / 4
Simple rules to simplify syntax (towards DSL)
36. IMPLICITS/I
class MargheritaList(val n: Int) {
def margheritas = {
var list: List[Pizza] = List()
for (i <- 1 to n)
list = list :+ Margherita
list
}
}
using a var with an immutable list
note the generics
using collection operator :+
37. IMPLICITS/II
val order = new MargheritaList(4).margheritas()
a bit verbose..let's introduce implicit conversion
implicit def fromIntToMargherita(n: Int) = new MargheritaList(n)
compiler implicity does conversion for us
val order = 4.margheritas()
or better
val order = 4 margheritas
38. A PIZZA DSL
val pizza = Margherita
Pino preparePizza pizza
Oven cook pizza
pizza / 6
Question. How much keywords do you see?
39. THE END
Thanks for following, slides and video on
http://cosenonjaviste.it