
Transcription
SCALA CRASH COURSEHolden Karau - @holdenkarau
Scala vs Java API vs PythonSpark was originally written in Scala, whichallows concise function syntax andinteractive useJava API added for standalone applicationsPython API added more recently along with aninteractive shell.This course: mostly Scala, some translations shown to Java & Python
OutlineIntroduction to Scala & functional programmingA picture of a catCoffee Break**The coffee break may or may not be a lie.
Introduction to ScalaWhat is Scala?Functions in ScalaOperating on collections in Scala
About ScalaHigh-level language for the JVM Object oriented functional programmingStatically typed Comparable in speed to Java* Type inference saves us from having to writeexplicit types most of the timeInteroperates with Java Can use any Java class (inherit from, etc.) Can be called from Java code
Best way to Learn ScalaInteractive scala shell (just type scala)Supports importing libraries, tab completing, and allof the constructs in the languagehttp://www.scala-lang.org/
Quick Tour of ScalaDeclaring variables:var x: Int 7var x 7 // type inferredval y “hi” // read-onlyJava equivalent:int x 7;Functions:def square(x: Int): Int x*xdef square(x: Int): Int {x*x}def announce(text: String) {println(text)}Java equivalent:int square(int x) {return x*x;}final String y “hi”;void announce(String text) {System.out.println(text);}
Scala functions (closures)(x: Int) x 2 // full version
Scala functions (closures)(x: Int) x 2 // full versionx x 2 // type inferred
Scala functions (closures)(x: Int) x 2 // full versionx x 2 // type inferred 2 // placeholder syntax (each argument must be usedexactly once)
Scala functions (closures)(x: Int) x 2 // full versionx x 2 // type inferred 2 // placeholder syntax (each argument must be usedexactly once)x { // body is a block of codeval numberToAdd 2x numberToAdd}
Scala functions (closures)(x: Int) x 2 // full versionx x 2 // type inferred 2 // placeholder syntax (each argument must be usedexactly once)x { // body is a block of codeval numberToAdd 2x numberToAdd}// Regular functionsdef addTwo(x: Int): Int x 2
Quick Tour of Scala Part 2(electric boogaloo)Processing collections with functional programmingval lst List(1, 2, 3)list.foreach(x println(x)) // prints 1, 2, 3list.foreach(println)// samelist.map(x x 2)list.map( 2)// returns a new List(3, 4, 5)// samelist.filter(x x % 2 1)// returns a new List(1, 3)list.filter( % 2 1)// samelist.reduce((x, y) x y) // 6list.reduce( )// sameAll of these leave the list unchanged as it is immutable.
Functional methods on collectionsThere are a lot of methods on Scala collections, just google Scala Seq or ala.collection.SeqMethod on Seq[T]Explanationmap(f: T U): Seq[U]Each element is result of fflatMap(f: T Seq[U]): Seq[U]One to many mapfilter(f: T Boolean): Seq[T]Keep elements passing fexists(f: T Boolean): BooleanTrue if one element passes fforall(f: T Boolean): BooleanTrue if all elements passreduce(f: (T, T) T): TMerge elements using fgroupBy(f: T K): Map[K, List[T]]Group elements by fsortBy(f: T K): Seq[T]Sort elements .
Cat picture from eak-173043455
About Scala High-level language for the JVM Object oriented functional programming Statically typed Comparable in speed to Java* Type inference saves us from having to write