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?

Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 

Was ist angesagt? (20)

Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Introduction To Java.
Introduction To Java.Introduction To Java.
Introduction To Java.
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Identifiers
Identifiers Identifiers
Identifiers
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
 
Data types in java
Data types in javaData types in java
Data types in java
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Java & advanced java
Java & advanced javaJava & advanced java
Java & advanced java
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 

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

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Kürzlich hochgeladen (20)

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
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
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
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
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
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?
  • 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