An Introduction to NLP4L (Scala by the Bay / Big Data Scala 2015)
Introduction to Scala for hackersnl gathering
-
Upload
peter-maas -
Category
Technology
-
view
753 -
download
0
description
Transcript of Introduction to Scala for hackersnl gathering
eBay Inc. confidential
Peter Maas
2011-Q2
What it is and why it rocks
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
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.
4 eBay Inc. confidential
Scala
• Scalable Language
• Statically typed
• First class citizen on the JVM
• Multi paradigm: OO, FP
5 eBay Inc. confidential
Who uses it
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. */
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 _ =>
}
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)
9 eBay Inc. confidential
Collections :: Immutable
10 eBay Inc. confidential
Collections :: Mutable
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)
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
13 eBay Inc. confidential
More concepts
• Case classes
• Structural Typing
• Traits
• Implicit conversions
• Operators are functions
• Actors
• Laziness / Streams
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.
15 eBay Inc. confidential
Actors
• Scalas’ main construct for parallelizing tasks
• Actors are concurrent processes that communicate by exchanging messages
• Akka!
16 eBay Inc. confidential
Frameworks
• All existing Java frameworks
• Database– Querulous– Casbah
• Web– Play! Scala module– Lift
• Messaging– Kafka– Kestrel
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.
18 eBay Inc. confidential
More info
• http://scala-lang.org
• Programming in Scala 2nd edition (Odersky)
• twitter.com/p3t0r
• log4p.com
19 eBay Inc. confidential
Q&A (YES: WE ARE HIRING)
One more thing: