Scala 101-bcndevcon

1,409 views

Published on

This is a reviewed version of our scala-101 presentation with some extra contents. We prepared this version for the Barcelona Developer Conference 2013.
See the original at: http://www.slideshare.net/IgnasiMarimonClos/scala-101

Published in: Technology, News & Politics
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,409
On SlideShare
0
From Embeds
0
Number of Embeds
571
Actions
Shares
0
Downloads
11
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Scala 101-bcndevcon

  1. 1. Introduction to Scala Programming Language Barcelona Developers Conference 2013 (#bcd13) Scala Developers Barcelona (#scalabcn) Jordi Pradel (@agile_jordi) Ignasi Marimon-Clos (@ignasi35) Friday, November 8, 13
  2. 2. thanks! Friday, November 8, 13
  3. 3. about me n. /iŋ'nazi/ - @ignasi35 1) problem solver, Garbage Collector, mostly java, learning scala, some agile 2) kayaker 3) under construction 4) wears glasses Friday, November 8, 13
  4. 4. about me jordi pradel - @agile_jordi 1) agile software developer 2) agile punk 3) scala practitioner 4) glasses, braid Friday, November 8, 13
  5. 5. about you Friday, November 8, 13
  6. 6. Some Context Friday, November 8, 13
  7. 7. What they say “If I were to pick a language today other than Java it would be Scala” James Gosling (created Java) “I can honestly say if someone had shown me the Programming Scala book by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I’d probably nave never created Groovy” James Strachan (created Groovy) Friday, November 8, 13
  8. 8. What they say “What’s next? LISP” Paolo Perrotta at Baruco 2013 http://www.youtube.com/watch?v=ahh-QkttjuM&t=115 Friday, November 8, 13
  9. 9. Friday, November 8, 13
  10. 10. http://www.indeed.com/jobtrends?q=scala%2C+ruby%2C+python&l=&relative=1 Friday, November 8, 13
  11. 11. http://www.itjobswatch.co.uk/jobs/uk/scala.do Friday, November 8, 13
  12. 12. http://www.itjobswatch.co.uk/jobs/uk/scala.do Friday, November 8, 13
  13. 13. Start with why Friday, November 8, 13
  14. 14. A Problem: • A service to process a Basket Purchase • buy items online • Pay via VISA • Pay via PayPal Friday, November 8, 13
  15. 15. This is your (jee) life public class PurchaseProcessorFactory { public PurchaseProcessor forPayPal() { return new PurchaseProcessor() { @Override public void process(Purchase t) { // process t using PayPal system } }; } } public PurchaseProcessor forVisa() { return new PurchaseProcessor() { @Override public void process(Purchase t) { // process t using Visa } }; } Friday, November 8, 13
  16. 16. This is your (jee) life package com.meetup.java; package com.meetup.java; public class Item { import java.util.Collections; import java.util.List; private final String _productId; private final int _rupees; public Item(String _productId, int _rupees) { super(); this._productId = _productId; this._rupees = _rupees; } @Override public List<Item> getItems() { public String toString() { return Collections.unmodifiableList(_items); return "Item [_productId=" + _productId + ", _rupees=" + _rupees + "]"; } } public class Purchase { private final List<Item> _items; public Purchase(List<Item> items) { super(); _items = items; } @Override @Override public String toString() { public int hashCode() { return "Purchase [_items=" + _items + "]"; final int prime = 31; } int result = 1; result = prime * result + ((_productId == null) ? 0 : _productId.hashCode()); @Override public int hashCode() { result = prime * result + _rupees; final int prime = 31; return result; int result = 1; } result = prime * result + ((_items == null) ? 0 : _items.hashCode()); return result; @Override } public boolean equals(Object obj) { if (this == obj) @Override return true; public boolean equals(Object obj) { if (obj == null) if (this == obj) return false; return true; if (getClass() != obj.getClass()) if (obj == null) return false; return false; Item other = (Item) obj; if (getClass() != obj.getClass()) if (_productId == null) { if (other._productId != null) return false; } else if (!_productId.equals(other._productId)) Friday, November 8, 13 return false;
  17. 17. What’s wrong there? • class per file (sort of) • unnecessary redefinition of stuff • duplicate intent • (DRY is not only about copy/paste) • unmaintainability • JAVA Friday, November 8, 13
  18. 18. OH: “If a Notary could write code, she’d use JAVA” Friday, November 8, 13
  19. 19. 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 forPayPal = new PurchaseProcessor { def process(p : Purchase) = println(p) // do real stuff here } val forVisa = new PurchaseProcessor { def process(p : Purchase) = println(p) // do real stuff here } } Friday, November 8, 13 fits in one slide (and fixes one bug!)
  20. 20. Entering decompiler Friday, November 8, 13
  21. 21. case classes case class Item(productId : String, rupees : Int) case class Purchase(items : List[Item]) • all boilerplate gone • no equals/hashCode/toString maintenance • immutable • named params • optional params (not shown) • pattern matching Friday, November 8, 13
  22. 22. pater-WAT ?? Friday, November 8, 13
  23. 23. Pattern Matching def run(input : Any) = { input match { case s : String => case i : Int if(i<0) case i : Int => case _ => } } println(s) => println("A negative int: " + i) println("A number: " + i) println("something else") // aka default: > run: (input: Any)Unit run("A long time ago in a galaxy far far away...") //> A long time ago / /> in a gala... run(234) run(() => 42) //> A number: 234 //> something else Worksheet rocks! REPL rocks! Friday, November 8, 13
  24. 24. Functions! run(() => 42) val f = () => 42 run(f) • Functions as first class citizens • Functional Progamming! Bitches! • parens + arrow + statements • ‘return’ keyword is optional • Friday, November 8, 13 SOLUTION: A function with no params that simply returns ’42’
  25. 25. 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 Friday, November 8, 13
  26. 26. Back to coding! Friday, November 8, 13
  27. 27. Syntactic Sugar • • Dot is replaceable with whitespace • • • Functions can be declared inline Parens are optional and usually replaceable with curly braces return and semicolon are optional _ l map { twice andThen thrice } l.map((i:Int) => twice.andThen(thrice)(i)) Friday, November 8, 13
  28. 28. so far • • case classes • traits • multi-hierarchy • pattern matching conciseness Friday, November 8, 13 • Functions • immutability • monads * • spaces/parens/dots • return keyword
  29. 29. Friday, November 8, 13
  30. 30. conclusions • • • • • • Friday, November 8, 13 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...
  31. 31. 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 Friday, November 8, 13
  32. 32. other stuff • • • • • • • • Friday, November 8, 13 Macros Currying, partially applied functions, ... Structural Typing implicits Value classes Lazy evaluation Tail recursion ... (see? we _do_ have duck typing!)
  33. 33. want more? • • • • • • Friday, November 8, 13 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 http://www.meetup.com/Scala-Developers-Barcelona/
  34. 34. thanks! Friday, November 8, 13
  35. 35. Oh! And we know what monads are. ;-) Friday, November 8, 13

×