Introduction to Scala for hackersnl gathering
-
Upload
peter-maas -
Category
Technology
-
view
753 -
download
0
description
Transcript of Introduction to Scala for hackersnl gathering
![Page 1: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/1.jpg)
eBay Inc. confidential
Peter Maas
2011-Q2
What it is and why it rocks
![Page 2: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/2.jpg)
2 eBay Inc. confidential
Goals
• You’ll learn the what & why of Scala
• You’ll learn what basic Scala programs look like
• You’ll get an insight in how deep Scala can be
![Page 3: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/3.jpg)
3 eBay Inc. confidential
Peter Maas
• Technical team lead at eBay Classifieds group working at Marktplaats.
• Interested in programming languages in general; every new concept you learn might help you solve problems in a smarter / cleaner way.
• Hasn’t worked on a MS Windows machine for quite a while.
• Background in Sound & Music, specialized in pattern recognition. But spend the last decade on web development.
• Might know me from: – Marktplaats.nl (admarkt)– VPRO (3voor12, cinema.nl, …)– Kennis- SURFnet (Video portals, Wikiwijs, Leraar24)– DBGA (Erfpacht register)– RaboBank (RaboMobiel)– Ruby, Clojure, Devnology or DuSe usergroups
• Proud dad of two beautiful kids.
![Page 4: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/4.jpg)
4 eBay Inc. confidential
Scala
• Scalable Language
• Statically typed
• First class citizen on the JVM
• Multi paradigm: OO, FP
![Page 5: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/5.jpg)
5 eBay Inc. confidential
Who uses it
![Page 6: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/6.jpg)
6 eBay Inc. confidential
Basic features
Immutable value:
val name:Type = …
Mutable value:
var name:Type = …
A function:
def functionName(argName:argType):returnType = /* functionBody */
Class
class ClassName(constructorArg:argType) { }
Object:
object ObjectName { }
Tuple
(1,2) or (1,2,"booh",4,5) /* access t._1 , t._3 etc. */
![Page 7: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/7.jpg)
7 eBay Inc. confidential
Basic control flow
If:
if(x < 5) doA else doB
For:
for(x <- 0 to 5) println(x)
for(x <- 0 to 5) yield(x * 2) /* returns collections containing x*2 vals */
for(x <- 0 to 5; y <- 0 to x) yield(x * y) /* comprehensions */
Pattern Matching:
something match {
case a:Int => println(a)
case (a:Int, b:String) =>
case x :: xs => /* head, tail */
case _ =>
}
![Page 8: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/8.jpg)
8 eBay Inc. confidential
(Case) Classes
Java:public class Person {
public final String name;
public final int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
// TODO: implement equals
// TODO: implement hashCode
}
Scala:
case class Person(val name:String, val age:Int)
![Page 9: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/9.jpg)
9 eBay Inc. confidential
Collections :: Immutable
![Page 10: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/10.jpg)
10 eBay Inc. confidential
Collections :: Mutable
![Page 11: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/11.jpg)
11 eBay Inc. confidential
Collections :: Operations
Traversing
list.foreach( it => println(it) )
list.foreach( println(_) )
Transforming
list.map( _ * 2)
urls.par.map(io.Source.fromURL(_).mkString(""))
Reducing / Folding
list.reduceLeft(_+_) /* f(f(f(a,b),c),d), f = + */
list.foldLeft(5)(_+_) /* f(f(f(f(5,a),b),c),d), f = + */
list.foldRight(List[Int]())((i,l) => if(i%2 == 0) i :: l else l )
Filtering
list.filterNot(_%2 == 0)
Sorting
list.sorted, list.sortBy(it => it.name)
![Page 12: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/12.jpg)
12 eBay Inc. confidential
Collections :: Operations
Grouping
(1 to 10).groupBy(v => v%5) /* Map[Int, Vector[Int]] */
Zip
List(1,2,3,4).zip(List(5,6,7,8)) /* List((1,5), (2,6), (3,7), (4,8)) */
Much more:
diff, distinct, exists, grouped, intersect, slice
![Page 13: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/13.jpg)
13 eBay Inc. confidential
More concepts
• Case classes
• Structural Typing
• Traits
• Implicit conversions
• Operators are functions
• Actors
• Laziness / Streams
![Page 14: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/14.jpg)
14 eBay Inc. confidential
Structural typing
Statically typed version of duck typing
Immutable value:
def feed(anyThing: { def eat(f:Food):Unit }) = {
anyThing.eat(myFood)
}
Keeps coupling as low as possible.
![Page 15: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/15.jpg)
15 eBay Inc. confidential
Actors
• Scalas’ main construct for parallelizing tasks
• Actors are concurrent processes that communicate by exchanging messages
• Akka!
![Page 16: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/16.jpg)
16 eBay Inc. confidential
Frameworks
• All existing Java frameworks
• Database– Querulous– Casbah
• Web– Play! Scala module– Lift
• Messaging– Kafka– Kestrel
![Page 17: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/17.jpg)
17 eBay Inc. confidential
Note on tools
• During this presentations you’ve seen SBT, VIM and Intellij Idea in action
• All major (Java) build systems include support (Maven, Ant, Grails, Buildr, SBT)
• Scala IDE (Eclipse based, maintained by TypeSafe)
• Scala plugin for Intellij IDEA
• Scala plugin for NetBeans
And yes… it’s not 100% on par with tooling Java people are generally used to; but still good enough to work in a comfortable way.
![Page 18: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/18.jpg)
18 eBay Inc. confidential
More info
• http://scala-lang.org
• Programming in Scala 2nd edition (Odersky)
• twitter.com/p3t0r
• log4p.com
![Page 19: Introduction to Scala for hackersnl gathering](https://reader035.fdocuments.in/reader035/viewer/2022062312/554f5cc5b4c905b9508b54c0/html5/thumbnails/19.jpg)
19 eBay Inc. confidential
Q&A (YES: WE ARE HIRING)
One more thing: