scala for dummies
TRANSCRIPT
MADRID · MAR · 2015
Scala Programming @ Madrid
About us
Javier Santos @jpaniego
«Hay dos formas de programar sin errores; solo la tercera funciona»
Alan J Perlis
David Vallejo @dvnavarro
«Una expresión regular es una expresión que no es del todo buena, pero tampoco del todo mala.»
@borjamonserrano
MADRID · MAR · 2015
Scala Programming @ Madrid
What’s Scala?
According Wikipedia:
Scala (/ˈskɑːlə/ SKAH-lə)[6] is an object-functional[7] programming language for general software
applications. Scala has full support for functional programming and a very strong static type system. This
allows programs written in Scala to be very concise and thus smaller in size than other general-purpose
programming languages. Many of Scala's design decisions were inspired by criticism over the shortcomings
of Java.[5]
MADRID · MAR · 2015
Scala Programming @ Madrid
What’s Scala?
● General purpose language that runs on the JVM.
● Martin Odersky
● Multiple Inheritance
● Multi-paradigm: functional & object-oriented
● Typesafe (2011)
● TIOBE (41st position)
MADRID · MAR · 2015
Scala Programming @ Madrid
Why so fashionable these days?
● Useful tools to build concurrent systems○ More cores, more concurrency○ Avoid side effects using functional programming and immutable values○ Actor paradigm with the toolkit called Akka
● Awesome frameworks and tool-kits: Play Framework, Akka, Spark, Spray ...
● Others○ Powerful mechanisms for embedding DSL like, for example, implicits○ The best of functional world and the best of OO world○ Elegant & concise
MADRID · MAR · 2015
Scala Programming @ Madrid
Developer environment
● REPL (Read-eval-print loop)http://scala-lang.org/download/
● SBThttp://www.scala-sbt.org/
● IDES: Eclipse http://scala-ide.org/download/sdk.htmlIntellij https://www.jetbrains.com/idea/features/scala.htmlNetBeans http://wiki.netbeans.org/Scala
MADRID · MAR · 2015
Scala Programming @ Madrid
val vs var● Type inference● var: variables (mutable)
● val: values (immutable)
● lazy vals: Not evaluated until necessary
MADRID · MAR · 2015
Scala Programming @ Madrid
val vs var● vals are always immutable?
Stateful objects (mutable collections for example) (watch out with side effects)
MADRID · MAR · 2015
Scala Programming @ Madrid
Methods/functions
● Type inference● Only braces means Unit
● No return necessary
● Variable args number (*)
MADRID · MAR · 2015
Scala Programming @ Madrid
What about class?● A class may contain
○ attributes (var/val)○ methods (def)○ expressions that
will be evaluated at instantiation time.
MADRID · MAR · 2015
Scala Programming @ Madrid
What about class?
● A simple class with no parameters
MADRID · MAR · 2015
Scala Programming @ Madrid
What about class?
● A simple class with parameter accessors
MADRID · MAR · 2015
Scala Programming @ Madrid
What about abstract class?
● It cannot be instantiated, unless ...
MADRID · MAR · 2015
Scala Programming @ Madrid
What about case class?
● A case class provides:○ parameter accessors○ companion object with apply and unapply○ extends from Serializable○ copy method
● ...but it cannot be extended (We can handle that)
MADRID · MAR · 2015
Scala Programming @ Madrid
Rich interfaces: trait
● Methods can be implemented.● Multiple inheritance.
MADRID · MAR · 2015
Scala Programming @ Madrid
Inheritance: mixing in stuff...
● Linearization (Watch out the order)
MADRID · MAR · 2015
Scala Programming @ Madrid
What about nulls in Scala?
● Use option instead● Two possible values: Some(t) and None
MADRID · MAR · 2015
Scala Programming @ Madrid
Options in Scala
● Besides it’s iterable!● Nice functional methods like fold.
MADRID · MAR · 2015
Scala Programming @ Madrid
Conditional statements: match
● Similar to a Java switch...
MADRID · MAR · 2015
Scala Programming @ Madrid
Conditional statements: match
● ...except that it’s not only a switch :-)● Pattern matching.
MADRID · MAR · 2015
Scala Programming @ Madrid
Conditional statements: match
● So it’s great, but be carefull with
MADRID · MAR · 2015
Scala Programming @ Madrid
Loop statements: while
● Not very commonly used (vars and so on…)
MADRID · MAR · 2015
Scala Programming @ Madrid
Loop statements: for
● for-comprehensions: syntactic sugar for
MADRID · MAR · 2015
Scala Programming @ Madrid
Loop statements: for
● Typical example:
Is this purely functional?
MADRID · MAR · 2015
Scala Programming @ Madrid
Your new friend: fold
● Two signatures: foldLeft and foldRight (iterative vs. recursive)
● Same example:
MADRID · MAR · 2015
Scala Programming @ Madrid
Other common friends: map
● Convert each element to another (or same) type.
● Example:
MADRID · MAR · 2015
Scala Programming @ Madrid
Other common friends: flatMap
● Map each element to some monad-like type and flatten them
● Example:
MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Type alias
● Useful for specifying anonymous tuple types
MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Higher order functions
● Javascript ninjas gonna like…
MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Infix notation, semicolon inference...
● Allows writing readable code● Specially useful for DSLs.
MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Infix notation, semicolon inference...
MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Implicits
● Defined by scope (be careful with ambiguity)● Specially difficult to trace: use only in very located places
MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Self types
● Used for disambiguating with duplicated val/function names
MADRID · MAR · 2015
Scala Programming @ Madrid
Conclusions
● Steep learning curve
● It's hard to know which is the best way to do something
● Language expanding. More and more Scala enthusiasts.
● Mix the functional world and the OO world :)
MADRID · MAR · 2015
Scala Programming @ Madrid
scalerablog.wordpress.com
@scalerablog