SlideShare uma empresa Scribd logo
1 de 25
Baixar para ler offline
Scala Developers
Barcelona (#scalabcn)
Welcome Scala Newbies
Autumn Edition (sept 2013)
sponsored by special appearance Jordi Pradel (@agile_jordi)
Ignasi Marimon-Clos (@ignasi35)
divendres 27 de setembre de 13
thanks!
divendres 27 de setembre de 13
about me
n. /iŋ'nazi/
1) problem solver, Garbage Collector, mostly
java, learning scala, some agile
2) kayaker
3) under construction
4) wears glasses
divendres 27 de setembre de 13
about me
jordi pradel - @agile_jordi
1) agile software developer
2) agile punk
3) scala practitioner
4) glasses, braid
divendres 27 de setembre de 13
about you
divendres 27 de setembre de 13
Start with why
divendres 27 de setembre de 13
This is your (jee) life
public class PurchaseProcessorFactory {
	 public PurchaseProcessor forPoyPol() {
	 	 return new PurchaseProcessor() {
	 	 	 @Override
	 	 	 public void process(Purchase t) {
	 	 	 	 // process t using PoyPol system
	 	 	 }
	 	 };
	 }
	 public PurchaseProcessor forViso() {
	 	 return new PurchaseProcessor() {
	 	 	 @Override
	 	 	 public void process(Purchase t) {
	 	 	 	 // process t using Viso
	 	 	 }
	 	 };
	 }
}
divendres 27 de setembre de 13
This is your (jee) life
package com.meetup.java;
import java.util.Collections;
import java.util.List;
public class Purchase {
	 private final List<Item> _items;
	 public Purchase(List<Item> items) {
	 	 super();
	 	 _items = items;
	 }
	 public List<Item> getItems() {
	 	 return Collections.unmodifiableList(_items);
	 }
	 @Override
	 public String toString() {
	 	 return "Purchase [_items=" + _items + "]";
	 }
	 @Override
	 public int hashCode() {
	 	 final int prime = 31;
	 	 int result = 1;
	 	 result = prime * result + ((_items == null) ? 0 : _items.hashCode());
	 	 return result;
	 }
	 @Override
	 public boolean equals(Object obj) {
	 	 if (this == obj)
	 	 	 return true;
	 	 if (obj == null)
	 	 	 return false;
	 	 if (getClass() != obj.getClass())
package com.meetup.java;
public class Item {
	 private final String _productId;
	 private final int _rupees;
	 public Item(String _productId, int _rupees) {
	 	 super();
	 	 this._productId = _productId;
	 	 this._rupees = _rupees;
	 }
	 @Override
	 public String toString() {
	 	 return "Item [_productId=" + _productId + ", _rupees=" + _rupees + "]";
	 }
	 @Override
	 public int hashCode() {
	 	 final int prime = 31;
	 	 int result = 1;
	 	 result = prime * result
	 	 	 	 + ((_productId == null) ? 0 : _productId.hashCode());
	 	 result = prime * result + _rupees;
	 	 return result;
	 }
	 @Override
	 public boolean equals(Object obj) {
	 	 if (this == obj)
	 	 	 return true;
	 	 if (obj == null)
	 	 	 return false;
	 	 if (getClass() != obj.getClass())
	 	 	 return false;
	 	 Item other = (Item) obj;
	 	 if (_productId == null) {
	 	 	 if (other._productId != null)
	 	 	 	 return false;
	 	 } else if (!_productId.equals(other._productId))
	 	 	 return false;divendres 27 de setembre de 13
What’s wrong there?
• class per file (sort of)
• unnecessary redefinition of stuff
• duplicate intent
• (DRY is not only about copy/paste)
• unmaintainability
• JAVA
divendres 27 de setembre de 13
Let’s redo that
package com.meetup.scala
case class Item(productId : String, rupees : Int)
case class Purchase(items : List[Item])
trait Processor[T] {
def process(t : T) : Unit
}
trait PurchaseProcessor extends Processor[Purchase]
object PurchaseProcessor {
val forPoyPol = new PurchaseProcessor {
def process(p : Purchase) = println(p) // do real stuff here
}
val forViso = new PurchaseProcessor {
def process(p : Purchase) = println(p) // do real stuff here
}
}
fits in one slide (and fixes one bug!)
divendres 27 de setembre de 13
Entering decompiler
(Insert picture of Morpheus holding red pill and blue pill)
divendres 27 de setembre de 13
case classes
• all boilerplate gone
• no equals/hashCode/toString maintenance
• immutable
• named params
• optional params (not shown)
• pattern matching
divendres 27 de setembre de 13
pater-WAT ??
divendres 27 de setembre de 13
Pattern Matching
def run(input : Any) = {
input match {
case s : String => println(s)
case i : Int => println("A number: " + i)
case _ => println("something else")
}
} //> run: (input: Any)Unit
run("A long time ago in a galaxy far far away...")
//> A long time ago in a galaxy
/ /> far far away...
run(234) //> A number: 234
run(() => 42) //> something else
Worksheet rocks! REPL rocks!
divendres 27 de setembre de 13
Functions!
run(() => 42) //> something else WTF???
• Functions as first class citizens
• Functional Progamming! Bitches!
• parens + arrow + statements
• ‘return’ keyword is optional
• SOLUTION:A function with no params that simply returns ’42’
divendres 27 de setembre de 13
Functional Programming
• referential transparency
• immutability (see case classes)
• no side effects
• separate data from behavior
• monads *
* We don’t know what monads are but a talk about
FP requires using the word monad
divendres 27 de setembre de 13
Back to coding!
divendres 27 de setembre de 13
so far
• conciseness
• case classes
• traits
• multi-hierarchy
• pattern matching
• Functions
• immutability
• monads *
• spaces/parens/dots
• return keyword
divendres 27 de setembre de 13
divendres 27 de setembre de 13
Yak shaving
• Any seemingly pointless activity which is actually necessary
to solve a problem which solves a problem which, several
levels of recursion later, solves the real problem you're
working on.
http://www.urbandictionary.com/define.php?term=yak%20shaving
divendres 27 de setembre de 13
conclusions
• A lot of syntactic sugar
• Many small features but all related
• functional!
• object oriented (unlike clojure/erlang/...)
• typesafe without the pain
• Classes, Objects, Companions, Traits...
divendres 27 de setembre de 13
other stuff
• Macros
• Currying, partially applied functions, ...
• Structural Typing (see? we _do_ have duck typing!)
• implicits
• Value classes
• Lazy evaluation
• Tail recursion
• ...
divendres 27 de setembre de 13
want more?
• Intro to scala: http://scalacamp.pl/intro/#/start
• Twitter’s Scala School: http://twitter.github.io/scala_school/
• 99 problems in scala: http://aperiodic.net/phil/scala/s-99/
• Scala Puzzles: http://scalapuzzlers.com/
• “Programming in Scala” (1st ed free online: http://www.artima.com/pins1ed/)
• Scala Developers Barcelona Meetup
divendres 27 de setembre de 13
thanks!
divendres 27 de setembre de 13
Oh! And we know what monads are. ;-)
divendres 27 de setembre de 13

Mais conteúdo relacionado

Mais procurados

Java script object model
Java script object modelJava script object model
Java script object model
James Hsieh
 
Converting your JS library to a jQuery plugin
Converting your JS library to a jQuery pluginConverting your JS library to a jQuery plugin
Converting your JS library to a jQuery plugin
thehoagie
 

Mais procurados (20)

JavaScript operators
JavaScript operatorsJavaScript operators
JavaScript operators
 
Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
 
Java script object model
Java script object modelJava script object model
Java script object model
 
JavaScript For CSharp Developer
JavaScript For CSharp DeveloperJavaScript For CSharp Developer
JavaScript For CSharp Developer
 
Beginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptBeginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScript
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
Converting your JS library to a jQuery plugin
Converting your JS library to a jQuery pluginConverting your JS library to a jQuery plugin
Converting your JS library to a jQuery plugin
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG June
 
Javascript ES6
Javascript ES6Javascript ES6
Javascript ES6
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testing
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
TDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with AndroidTDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with Android
 
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 

Semelhante a Scala 101

DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.pptDESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
AntoJoseph36
 
Front end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript coreFront end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript core
Web Zhao
 
CDI do básico ao avançado
CDI do básico ao avançadoCDI do básico ao avançado
CDI do básico ao avançado
Alberto Souza
 
Modeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based GamesModeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based Games
Ray Toal
 

Semelhante a Scala 101 (20)

Everything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insEverything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-ins
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
OOP
OOPOOP
OOP
 
25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
 
Scalable JavaScript
Scalable JavaScriptScalable JavaScript
Scalable JavaScript
 
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.pptDESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and Akka
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Front end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript coreFront end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript core
 
CDI do básico ao avançado
CDI do básico ao avançadoCDI do básico ao avançado
CDI do básico ao avançado
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
JavaScript Essentials
JavaScript EssentialsJavaScript Essentials
JavaScript Essentials
 
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
 
Getting the Most Out of jQuery Widgets
Getting the Most Out of jQuery WidgetsGetting the Most Out of jQuery Widgets
Getting the Most Out of jQuery Widgets
 
Modeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based GamesModeling Patterns for JavaScript Browser-Based Games
Modeling Patterns for JavaScript Browser-Based Games
 
Effective Java with Groovy - How Language Influences Adoption of Good Practices
Effective Java with Groovy - How Language Influences Adoption of Good PracticesEffective Java with Groovy - How Language Influences Adoption of Good Practices
Effective Java with Groovy - How Language Influences Adoption of Good Practices
 
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hocCiconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
 

Mais de Ignasi Marimon-Clos i Sunyol

Mais de Ignasi Marimon-Clos i Sunyol (9)

The Emperor Has No Docs (Geecon Oct'23)
The Emperor Has No Docs (Geecon Oct'23)The Emperor Has No Docs (Geecon Oct'23)
The Emperor Has No Docs (Geecon Oct'23)
 
Jeroglificos, Minotauros y la factura de la luz
Jeroglificos, Minotauros y la factura de la luzJeroglificos, Minotauros y la factura de la luz
Jeroglificos, Minotauros y la factura de la luz
 
Contributing to Akka (Hacktoberfest 2020)
Contributing to Akka (Hacktoberfest 2020)Contributing to Akka (Hacktoberfest 2020)
Contributing to Akka (Hacktoberfest 2020)
 
Contributing to OSS (Scalator 2020-01-22)
Contributing to OSS (Scalator 2020-01-22)Contributing to OSS (Scalator 2020-01-22)
Contributing to OSS (Scalator 2020-01-22)
 
Reactive Microsystems (Sw Crafters Barcelona 2018)
Reactive Microsystems (Sw Crafters Barcelona 2018)Reactive Microsystems (Sw Crafters Barcelona 2018)
Reactive Microsystems (Sw Crafters Barcelona 2018)
 
Lagom Workshop BarcelonaJUG 2017-06-08
Lagom Workshop  BarcelonaJUG 2017-06-08Lagom Workshop  BarcelonaJUG 2017-06-08
Lagom Workshop BarcelonaJUG 2017-06-08
 
Intro scala for rubyists (ironhack)
Intro scala for rubyists (ironhack)Intro scala for rubyists (ironhack)
Intro scala for rubyists (ironhack)
 
Functional Programming in JAVA 8
Functional Programming in JAVA 8Functional Programming in JAVA 8
Functional Programming in JAVA 8
 
Spray & Maven Intro for Scala Barcelona Developers Meetup
Spray & Maven Intro for Scala Barcelona Developers MeetupSpray & Maven Intro for Scala Barcelona Developers Meetup
Spray & Maven Intro for Scala Barcelona Developers Meetup
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

Scala 101

  • 1. Scala Developers Barcelona (#scalabcn) Welcome Scala Newbies Autumn Edition (sept 2013) sponsored by special appearance Jordi Pradel (@agile_jordi) Ignasi Marimon-Clos (@ignasi35) divendres 27 de setembre de 13
  • 2. thanks! divendres 27 de setembre de 13
  • 3. about me n. /iŋ'nazi/ 1) problem solver, Garbage Collector, mostly java, learning scala, some agile 2) kayaker 3) under construction 4) wears glasses divendres 27 de setembre de 13
  • 4. about me jordi pradel - @agile_jordi 1) agile software developer 2) agile punk 3) scala practitioner 4) glasses, braid divendres 27 de setembre de 13
  • 5. about you divendres 27 de setembre de 13
  • 6. Start with why divendres 27 de setembre de 13
  • 7. This is your (jee) life public class PurchaseProcessorFactory { public PurchaseProcessor forPoyPol() { return new PurchaseProcessor() { @Override public void process(Purchase t) { // process t using PoyPol system } }; } public PurchaseProcessor forViso() { return new PurchaseProcessor() { @Override public void process(Purchase t) { // process t using Viso } }; } } divendres 27 de setembre de 13
  • 8. This is your (jee) life package com.meetup.java; import java.util.Collections; import java.util.List; public class Purchase { private final List<Item> _items; public Purchase(List<Item> items) { super(); _items = items; } public List<Item> getItems() { return Collections.unmodifiableList(_items); } @Override public String toString() { return "Purchase [_items=" + _items + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((_items == null) ? 0 : _items.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) package com.meetup.java; public class Item { private final String _productId; private final int _rupees; public Item(String _productId, int _rupees) { super(); this._productId = _productId; this._rupees = _rupees; } @Override public String toString() { return "Item [_productId=" + _productId + ", _rupees=" + _rupees + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((_productId == null) ? 0 : _productId.hashCode()); result = prime * result + _rupees; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Item other = (Item) obj; if (_productId == null) { if (other._productId != null) return false; } else if (!_productId.equals(other._productId)) return false;divendres 27 de setembre de 13
  • 9. What’s wrong there? • class per file (sort of) • unnecessary redefinition of stuff • duplicate intent • (DRY is not only about copy/paste) • unmaintainability • JAVA divendres 27 de setembre de 13
  • 10. Let’s redo that package com.meetup.scala case class Item(productId : String, rupees : Int) case class Purchase(items : List[Item]) trait Processor[T] { def process(t : T) : Unit } trait PurchaseProcessor extends Processor[Purchase] object PurchaseProcessor { val forPoyPol = new PurchaseProcessor { def process(p : Purchase) = println(p) // do real stuff here } val forViso = new PurchaseProcessor { def process(p : Purchase) = println(p) // do real stuff here } } fits in one slide (and fixes one bug!) divendres 27 de setembre de 13
  • 11. Entering decompiler (Insert picture of Morpheus holding red pill and blue pill) divendres 27 de setembre de 13
  • 12. case classes • all boilerplate gone • no equals/hashCode/toString maintenance • immutable • named params • optional params (not shown) • pattern matching divendres 27 de setembre de 13
  • 13. pater-WAT ?? divendres 27 de setembre de 13
  • 14. Pattern Matching def run(input : Any) = { input match { case s : String => println(s) case i : Int => println("A number: " + i) case _ => println("something else") } } //> run: (input: Any)Unit run("A long time ago in a galaxy far far away...") //> A long time ago in a galaxy / /> far far away... run(234) //> A number: 234 run(() => 42) //> something else Worksheet rocks! REPL rocks! divendres 27 de setembre de 13
  • 15. Functions! run(() => 42) //> something else WTF??? • Functions as first class citizens • Functional Progamming! Bitches! • parens + arrow + statements • ‘return’ keyword is optional • SOLUTION:A function with no params that simply returns ’42’ divendres 27 de setembre de 13
  • 16. Functional Programming • referential transparency • immutability (see case classes) • no side effects • separate data from behavior • monads * * We don’t know what monads are but a talk about FP requires using the word monad divendres 27 de setembre de 13
  • 17. Back to coding! divendres 27 de setembre de 13
  • 18. so far • conciseness • case classes • traits • multi-hierarchy • pattern matching • Functions • immutability • monads * • spaces/parens/dots • return keyword divendres 27 de setembre de 13
  • 19. divendres 27 de setembre de 13
  • 20. Yak shaving • Any seemingly pointless activity which is actually necessary to solve a problem which solves a problem which, several levels of recursion later, solves the real problem you're working on. http://www.urbandictionary.com/define.php?term=yak%20shaving divendres 27 de setembre de 13
  • 21. conclusions • A lot of syntactic sugar • Many small features but all related • functional! • object oriented (unlike clojure/erlang/...) • typesafe without the pain • Classes, Objects, Companions, Traits... divendres 27 de setembre de 13
  • 22. other stuff • Macros • Currying, partially applied functions, ... • Structural Typing (see? we _do_ have duck typing!) • implicits • Value classes • Lazy evaluation • Tail recursion • ... divendres 27 de setembre de 13
  • 23. want more? • Intro to scala: http://scalacamp.pl/intro/#/start • Twitter’s Scala School: http://twitter.github.io/scala_school/ • 99 problems in scala: http://aperiodic.net/phil/scala/s-99/ • Scala Puzzles: http://scalapuzzlers.com/ • “Programming in Scala” (1st ed free online: http://www.artima.com/pins1ed/) • Scala Developers Barcelona Meetup divendres 27 de setembre de 13
  • 24. thanks! divendres 27 de setembre de 13
  • 25. Oh! And we know what monads are. ;-) divendres 27 de setembre de 13