The document discusses a Scala programming meetup in Madrid presented by David Vallejo and Javier Santos. It provides an overview of Scala including that it is a general purpose language that runs on the JVM, supports both object-oriented and functional programming, and is gaining popularity for building concurrent systems. The presentation covers various Scala features such as val vs var, methods, classes, traits, generics, pattern matching, loops, and more advanced topics like implicits. It concludes that Scala has a steep learning curve but allows mixing the best of functional and object-oriented worlds.
How to Choose the Right Laravel Development Partner in New York City_compress...
Scala for dummies
1. MADRID · MAR · 2015
Scala Programming @ Madrid
Scala
David Vallejo & Javier Santos
2. 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
3. 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]
4. MADRID · MAR · 2015
Scala Programming @ Madrid
What’s Scala?
According to the official website:
5. MADRID · MAR · 2015
Scala Programming @ Madrid
What’s Scala?
According to newbies:
6. 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)
7. 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
8. MADRID · MAR · 2015
Scala Programming @ Madrid
Who uses it?
9. MADRID · MAR · 2015
Scala Programming @ Madrid
Developer environment
● REPL (Read-eval-print loop)
http://scala-lang.org/download/
● SBT
http://www.scala-sbt.org/
● IDES:
Eclipse http://scala-ide.org/download/sdk.html
Intellij https://www.jetbrains.com/idea/features/scala.html
NetBeans http://wiki.netbeans.org/Scala
10. MADRID · MAR · 2015
Scala Programming @ Madrid
Features
11. MADRID · MAR · 2015
Scala Programming @ Madrid
val vs var
● Type inference
● var: variables (mutable)
● val: values (immutable)
● lazy vals: Not evaluated until necessary
12. MADRID · MAR · 2015
Scala Programming @ Madrid
val vs var
● vals are always immutable?
Stateful objects (mutable collections for example) (watch out with side effects)
13. MADRID · MAR · 2015
Scala Programming @ Madrid
Methods/functions
● Type inference
● Only braces means Unit
● No return necessary
● Variable args number (*)
14. 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.
15. MADRID · MAR · 2015
Scala Programming @ Madrid
What about class?
● A simple class with no parameters
16. MADRID · MAR · 2015
Scala Programming @ Madrid
What about class?
● A simple class with parameters
17. MADRID · MAR · 2015
Scala Programming @ Madrid
What about class?
● A simple class with parameter accessors
18. MADRID · MAR · 2015
Scala Programming @ Madrid
What about abstract class?
● It cannot be instantiated, unless ...
19. 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)
20. MADRID · MAR · 2015
Scala Programming @ Madrid
What about case class?
22. MADRID · MAR · 2015
Scala Programming @ Madrid
Rich interfaces: trait
● Methods can be implemented.
● Multiple inheritance.
23. MADRID · MAR · 2015
Scala Programming @ Madrid
Inheritance: mixing in stuff...
● Linearization (Watch out the order)
24. MADRID · MAR · 2015
Scala Programming @ Madrid
What about nulls in Scala?
● Use option instead
● Two possible values: Some(t) and None
25. MADRID · MAR · 2015
Scala Programming @ Madrid
Options in Scala
● Besides it’s iterable!
● Nice functional methods like fold.
26. MADRID · MAR · 2015
Scala Programming @ Madrid
Generics
● For classes and traits
27. MADRID · MAR · 2015
Scala Programming @ Madrid
Generics
● For methods
28. MADRID · MAR · 2015
Scala Programming @ Madrid
Generics
● Variance: C[T] != C[+T] != C[-T]
29. MADRID · MAR · 2015
Scala Programming @ Madrid
Conditional statements: if
30. MADRID · MAR · 2015
Scala Programming @ Madrid
Conditional statements: match
● Similar to a Java switch...
31. MADRID · MAR · 2015
Scala Programming @ Madrid
Conditional statements: match
● ...except that it’s not only a switch :-)
● Pattern matching.
32. MADRID · MAR · 2015
Scala Programming @ Madrid
Conditional statements: match
● So it’s great, but be carefull with
33. MADRID · MAR · 2015
Scala Programming @ Madrid
Loop statements: while
● Not very commonly used (vars and so on…)
34. MADRID · MAR · 2015
Scala Programming @ Madrid
Loop statements: for
● for-comprehensions: syntactic sugar for
35. MADRID · MAR · 2015
Scala Programming @ Madrid
Loop statements: for
● Typical example:
Is this purely functional?
36. MADRID · MAR · 2015
Scala Programming @ Madrid
Loops in Scala
37. MADRID · MAR · 2015
Scala Programming @ Madrid
Your new friend: fold
● Two signatures: foldLeft and foldRight (iterative vs. recursive)
● Same example:
38. MADRID · MAR · 2015
Scala Programming @ Madrid
Other common friends: map
● Convert each element to another (or same) type.
● Example:
39. MADRID · MAR · 2015
Scala Programming @ Madrid
Other common friends: flatMap
● Map each element to some monad-like type and flatten them
● Example:
40. MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Type alias
● Useful for specifying anonymous tuple types
41. MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Higher order functions
● Javascript ninjas gonna like…
42. MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Higher order functions
43. MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Higher order functions
44. MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Infix notation, semicolon inference...
● Allows writing readable code
● Specially useful for DSLs.
45. MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Infix notation, semicolon inference...
46. MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Implicits
47. 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
48. MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Self types
● Used for disambiguating with duplicated val/function names
49. 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 :)