0
Scala Developers
Barcelona (#scalabcn)
Welcome Scala Newbies
Autumn Edition (sept 2013)
sponsored by special appearance Jo...
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 co...
about me
jordi pradel - @agile_jordi
1) agile software developer
2) agile punk
3) scala practitioner
4) glasses, braid
div...
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 Pu...
This is your (jee) life
package com.meetup.java;
import java.util.Collections;
import java.util.List;
public class Purchas...
What’s wrong there?
• class per file (sort of)
• unnecessary redefinition of stuff
• duplicate intent
• (DRY is not only abo...
Let’s redo that
package com.meetup.scala
case class Item(productId : String, rupees : Int)
case class Purchase(items : Lis...
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...
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: "...
Functions!
run(() => 42) //> something else WTF???
• Functions as first class citizens
• Functional Progamming! Bitches!
• ...
Functional Programming
• referential transparency
• immutability (see case classes)
• no side effects
• separate data from...
Back to coding!
divendres 27 de setembre de 13
so far
• conciseness
• case classes
• traits
• multi-hierarchy
• pattern matching
• Functions
• immutability
• monads *
• ...
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...
conclusions
• A lot of syntactic sugar
• Many small features but all related
• functional!
• object oriented (unlike cloju...
other stuff
• Macros
• Currying, partially applied functions, ...
• Structural Typing (see? we _do_ have duck typing!)
• i...
want more?
• Intro to scala: http://scalacamp.pl/intro/#/start
• Twitter’s Scala School: http://twitter.github.io/scala_sc...
thanks!
divendres 27 de setembre de 13
Oh! And we know what monads are. ;-)
divendres 27 de setembre de 13
Upcoming SlideShare
Loading in...5
×

Scala 101

492

Published on

Presentation for the Scala Developer Barcelona meetups: Welcome Scala Newbies (sept 2013)

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
492
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Scala 101"

  1. 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. 2. thanks! divendres 27 de setembre de 13
  3. 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. 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. 5. about you divendres 27 de setembre de 13
  6. 6. Start with why divendres 27 de setembre de 13
  7. 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. 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. 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. 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. 11. Entering decompiler (Insert picture of Morpheus holding red pill and blue pill) divendres 27 de setembre de 13
  12. 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. 13. pater-WAT ?? divendres 27 de setembre de 13
  14. 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. 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. 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. 17. Back to coding! divendres 27 de setembre de 13
  18. 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. 19. divendres 27 de setembre de 13
  20. 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. 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. 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. 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. 24. thanks! divendres 27 de setembre de 13
  25. 25. Oh! And we know what monads are. ;-) divendres 27 de setembre de 13
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×