SlideShare ist ein Scribd-Unternehmen logo
1 von 52
Downloaden Sie, um offline zu lesen
MADRID · MAR · 2015
Scala Programming @ Madrid
Scala
David Vallejo & Javier Santos
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?
According to the official website:
MADRID · MAR · 2015
Scala Programming @ Madrid
What’s Scala?
According to newbies:
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
Who uses it?
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
MADRID · MAR · 2015
Scala Programming @ Madrid
Features
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 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
What about case class?
MADRID · MAR · 2015
Scala Programming @ Madrid
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
Generics
● For classes and traits
MADRID · MAR · 2015
Scala Programming @ Madrid
Generics
● For methods
MADRID · MAR · 2015
Scala Programming @ Madrid
Generics
● Variance: C[T] != C[+T] != C[-T]
MADRID · MAR · 2015
Scala Programming @ Madrid
Conditional statements: if
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
Loops in Scala
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: Higher order functions
MADRID · MAR · 2015
Scala Programming @ Madrid
More cool stuff: Higher order functions
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
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
MADRID · MAR · 2015
Scala Programming @ Madrid
Scala
David Vallejo & Javier Santos
MADRID · MAR · 2015
Scala Programming @ Madrid
scalerablog.wordpress.com
@scalerablog

Weitere ähnliche Inhalte

Was ist angesagt? (8)

FortiGate 60C Firewall Configuration Backup and Restore procedure Firmware v4...
FortiGate 60C Firewall Configuration Backup and Restore procedure Firmware v4...FortiGate 60C Firewall Configuration Backup and Restore procedure Firmware v4...
FortiGate 60C Firewall Configuration Backup and Restore procedure Firmware v4...
 
Java literals
Java literalsJava literals
Java literals
 
Congestión en Redes
Congestión en RedesCongestión en Redes
Congestión en Redes
 
Antenas
AntenasAntenas
Antenas
 
Propagation des ondes
Propagation des ondesPropagation des ondes
Propagation des ondes
 
Uni fiee scm sesion 05 propagación con canales móviles
Uni fiee scm sesion 05 propagación con canales móvilesUni fiee scm sesion 05 propagación con canales móviles
Uni fiee scm sesion 05 propagación con canales móviles
 
Lf
LfLf
Lf
 
Clean coding in plsql and sql, v2
Clean coding in plsql and sql, v2Clean coding in plsql and sql, v2
Clean coding in plsql and sql, v2
 

Andere mochten auch

Scala jeff
Scala jeffScala jeff
Scala jeff
jeff kit
 
Introduction to Asynchronous scala
Introduction to Asynchronous scalaIntroduction to Asynchronous scala
Introduction to Asynchronous scala
Stratio
 
Spark summit2014 techtalk - testing spark
Spark summit2014 techtalk - testing sparkSpark summit2014 techtalk - testing spark
Spark summit2014 techtalk - testing spark
Anu Shetty
 
Effective testing for spark programs Strata NY 2015
Effective testing for spark programs   Strata NY 2015Effective testing for spark programs   Strata NY 2015
Effective testing for spark programs Strata NY 2015
Holden Karau
 

Andere mochten auch (16)

Scala for dummies
Scala for dummiesScala for dummies
Scala for dummies
 
Scala Jump Start
Scala Jump StartScala Jump Start
Scala Jump Start
 
Stairway to scala flyer
Stairway to scala flyerStairway to scala flyer
Stairway to scala flyer
 
Codemotion 2015 - Akka voló sobre el nido del future
Codemotion 2015 - Akka voló sobre el nido del futureCodemotion 2015 - Akka voló sobre el nido del future
Codemotion 2015 - Akka voló sobre el nido del future
 
Scala@real life
Scala@real lifeScala@real life
Scala@real life
 
Codemotion 2014 Scala @real life
Codemotion 2014 Scala @real lifeCodemotion 2014 Scala @real life
Codemotion 2014 Scala @real life
 
Introducción a akka
Introducción a akkaIntroducción a akka
Introducción a akka
 
Scala @ Real Life Codemotion 2014
Scala @ Real Life Codemotion 2014Scala @ Real Life Codemotion 2014
Scala @ Real Life Codemotion 2014
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To Scala
 
Scala jeff
Scala jeffScala jeff
Scala jeff
 
Introduction to Asynchronous scala
Introduction to Asynchronous scalaIntroduction to Asynchronous scala
Introduction to Asynchronous scala
 
Spark summit2014 techtalk - testing spark
Spark summit2014 techtalk - testing sparkSpark summit2014 techtalk - testing spark
Spark summit2014 techtalk - testing spark
 
Unit testing of spark applications
Unit testing of spark applicationsUnit testing of spark applications
Unit testing of spark applications
 
Introducing Akka
Introducing AkkaIntroducing Akka
Introducing Akka
 
Effective testing for spark programs Strata NY 2015
Effective testing for spark programs   Strata NY 2015Effective testing for spark programs   Strata NY 2015
Effective testing for spark programs Strata NY 2015
 

Ähnlich wie Scala for dummies

20091226 名古屋SGGAE/J勉強会発表資料『Lift on GAE/J』
20091226 名古屋SGGAE/J勉強会発表資料『Lift on GAE/J』20091226 名古屋SGGAE/J勉強会発表資料『Lift on GAE/J』
20091226 名古屋SGGAE/J勉強会発表資料『Lift on GAE/J』
Ryo RKTM
 

Ähnlich wie Scala for dummies (20)

Introduction to spark
Introduction to sparkIntroduction to spark
Introduction to spark
 
Codemotion akka voló sobre el nido del future
Codemotion   akka voló sobre el nido del futureCodemotion   akka voló sobre el nido del future
Codemotion akka voló sobre el nido del future
 
Getting Started with Spring for GraphQL
Getting Started with Spring for GraphQLGetting Started with Spring for GraphQL
Getting Started with Spring for GraphQL
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Laskar: High-Velocity GraphQL & Lambda-based Software Development Model
Laskar: High-Velocity GraphQL & Lambda-based Software Development ModelLaskar: High-Velocity GraphQL & Lambda-based Software Development Model
Laskar: High-Velocity GraphQL & Lambda-based Software Development Model
 
Porting R Models into Scala Spark
Porting R Models into Scala SparkPorting R Models into Scala Spark
Porting R Models into Scala Spark
 
Estructurar y mantener aplicaciones Rails sin morir en el intento
Estructurar y mantener aplicaciones Rails sin morir en el intentoEstructurar y mantener aplicaciones Rails sin morir en el intento
Estructurar y mantener aplicaciones Rails sin morir en el intento
 
Spark
SparkSpark
Spark
 
JDT embraces lambda expressions
JDT embraces lambda expressionsJDT embraces lambda expressions
JDT embraces lambda expressions
 
Anatomy of spark catalyst
Anatomy of spark catalystAnatomy of spark catalyst
Anatomy of spark catalyst
 
Introduction to Spark ML Pipelines Workshop
Introduction to Spark ML Pipelines WorkshopIntroduction to Spark ML Pipelines Workshop
Introduction to Spark ML Pipelines Workshop
 
Modular GraphQL with Schema Stitching
Modular GraphQL with Schema StitchingModular GraphQL with Schema Stitching
Modular GraphQL with Schema Stitching
 
Java SE 8 library design
Java SE 8 library designJava SE 8 library design
Java SE 8 library design
 
Codemotion akka persistence, cqrs%2 fes y otras siglas del montón
Codemotion   akka persistence, cqrs%2 fes y otras siglas del montónCodemotion   akka persistence, cqrs%2 fes y otras siglas del montón
Codemotion akka persistence, cqrs%2 fes y otras siglas del montón
 
20091226 名古屋SGGAE/J勉強会発表資料『Lift on GAE/J』
20091226 名古屋SGGAE/J勉強会発表資料『Lift on GAE/J』20091226 名古屋SGGAE/J勉強会発表資料『Lift on GAE/J』
20091226 名古屋SGGAE/J勉強会発表資料『Lift on GAE/J』
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and Xitrum
 
Apache Jena Elephas and Friends
Apache Jena Elephas and FriendsApache Jena Elephas and Friends
Apache Jena Elephas and Friends
 
Scala Online Training.pdf
Scala Online Training.pdfScala Online Training.pdf
Scala Online Training.pdf
 
Efficient HTTP applications on the JVM with Ratpack - JDD 2015
Efficient HTTP applications on the JVM with Ratpack - JDD 2015Efficient HTTP applications on the JVM with Ratpack - JDD 2015
Efficient HTTP applications on the JVM with Ratpack - JDD 2015
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
 

Kürzlich hochgeladen

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Kürzlich hochgeladen (20)

Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 

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?
  • 21. MADRID · MAR · 2015 Scala Programming @ Madrid
  • 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 :)
  • 50. MADRID · MAR · 2015 Scala Programming @ Madrid
  • 51. MADRID · MAR · 2015 Scala Programming @ Madrid Scala David Vallejo & Javier Santos
  • 52. MADRID · MAR · 2015 Scala Programming @ Madrid scalerablog.wordpress.com @scalerablog