invitation to scala
DESCRIPTION
These are the slides for the talk I gave in Harvard IQSS's "tech talk" series. The commands and files for the demo ("REPL") part of the lecture can be obtained here: http://www.mbarsinai.com/blog/2013/08/04/invitation-to-scala/.TRANSCRIPT
![Page 1: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/1.jpg)
Introducing ScalaIQSS Tech-Talk, 2013-06-27Michael Bar-Sinai
Sunday, 4 August, 13
![Page 2: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/2.jpg)
Based in part on Scala for the impatient, Scala for java programmers tutorial at Typesafe.com, and Prof. Mayer Goldberg advanced programming class in Ben-Gurion university of the Negev, Israel
Sunday, 4 August, 13
![Page 3: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/3.jpg)
In a Few BulletsDeveloped in 2001, by Martin Odersky, EPFL Professor
Runs on the JVM (also, CLR)
Statically typed and with concise syntax
Object oriented and functional
Clean and elegant design
Strong collection framework
Expression-only, DSL friendly, advanced features
Sunday, 4 August, 13
![Page 4: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/4.jpg)
In a Few BulletsDeveloped in 2001, by Martin Odersky, EPFL Professor
Runs on the JVM (also, CLR)
Statically typed and with concise syntax
Object oriented and functional
Clean and elegant design
Strong collection framework
Expression-only, DSL friendly, advanced features
Sunday, 4 August, 13
![Page 5: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/5.jpg)
In a Few BulletsDeveloped in 2001, by Martin Odersky, EPFL Professor
Runs on the JVM (also, CLR)
Statically typed and with concise syntax
Object oriented and functional
Clean and elegant design
Strong collection framework
Expression-only, DSL friendly, advanced features
Sunday, 4 August, 13
![Page 6: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/6.jpg)
Sunday, 4 August, 13
![Page 7: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/7.jpg)
What is a...
Computation?
Sunday, 4 August, 13
![Page 8: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/8.jpg)
What is a computation?PROCEDURAL
Finite automaton working on an infinite tape
...0 1 110101110
Sunday, 4 August, 13
![Page 9: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/9.jpg)
What is a computation?PROCEDURAL FUNCTIONAL
Finite automaton working on an infinite tape
...0 1 110101110
Data flowing through a program
f(x) /2 Rf(6) 2 R
Sunday, 4 August, 13
![Page 10: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/10.jpg)
REPLSEE ACCOMPANYING FILES
Sunday, 4 August, 13
![Page 11: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/11.jpg)
List OperationsRun some binary operator on the list items and an intermediate results
fold, reduce, scan
Concurrent: aggregate,reduce
\ndlrow olleH
Sunday, 4 August, 13
![Page 12: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/12.jpg)
List OperationsRun some binary operator on the list items and an intermediate results
fold, reduce, scan
Concurrent: aggregate,reduce
\ndlrow olleH
Sunday, 4 August, 13
![Page 13: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/13.jpg)
Expressions vs. StatementsSTATEMENT
if ( map contains x ) { map(x) = computeNewX(map(x))} else { map(x) = computeNewX( 0 )}
Sunday, 4 August, 13
![Page 14: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/14.jpg)
Expressions vs. StatementsSTATEMENT
EXPRESSION
if ( map contains x ) { map(x) = computeNewX(map(x))} else { map(x) = computeNewX( 0 )}
map(x) = if ( map contains x ) computeNewX( map(x) ) else computeNewX( 0 )
Sunday, 4 August, 13
![Page 15: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/15.jpg)
Expressions vs. StatementsSTATEMENT
EXPRESSION
if ( map contains x ) { map(x) = computeNewX(map(x))} else { map(x) = computeNewX( 0 )}
map(x) = if ( map contains x ) computeNewX( map(x) ) else computeNewX( 0 )
ACTUAL SCALA CODEmap(x) = computeNewX( map.getOrElse(x,0) )
Sunday, 4 August, 13
![Page 16: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/16.jpg)
Class Syntax
Classes have a primary constructor that is part of the class definition
Clients can’t distinguish between getters and direct field access
Better privacy control
Sunday, 4 August, 13
![Page 17: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/17.jpg)
Classesclass Person( var name:String, val id:Int ) {! def greet = "Hello, my name is %s".format(name)}
class Person2( name:String, val id:Int ) {! def greet = "Hello, my name is %s".format(name)}
class Person3( name:String, val id:Int ) {! val greet = "Hello, my name is %s".format(name)}
Sunday, 4 August, 13
![Page 18: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/18.jpg)
Classes (cont.)
class Person4( aName:String, anId:Int ) {! private val id = anId! private[this] var pName = aName
! def name = pName! def name_=( newName:String ) { pName = newName }
! override def toString = "[Person4 id:%d name:%s]".format(id,name)}
Sunday, 4 August, 13
![Page 19: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/19.jpg)
Multiple InheritanceWould have been nice if it worked
It doesn’t
Java allowed only multipleinheritance of interfacesJDK8 would update this, slightly
Scala simulates multiple inheritance using type linearization
Would have been nice if it worked...it mostly does
A
B1 B2
C
A
B1 B2
C
Sunday, 4 August, 13
![Page 20: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/20.jpg)
TraitsAlmost like class:
Can have fields, protocols and behavior (implementations)
Can’t have constructor parameters
Can require minimal interface from implementing classes
Class can extend as many as needed
Types are generated at declaration point
Sunday, 4 August, 13
![Page 21: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/21.jpg)
Objectsand the absence of static
Replace the static parts in java
Manual declaration of a runtime singletons
Classes can have “companion objects” that have the same name
Good place for utility methods or special “apply” methods
App trait allows script-like behavior
Sunday, 4 August, 13
![Page 22: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/22.jpg)
Pattern Matching
Control structure that allows
switching
type inquiry
variable de-composition
Specialized Classes optimized for this
Sunday, 4 August, 13
![Page 23: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/23.jpg)
Simple Pattern Matching
def toFuzzyStringInt( i:Int ) = i match {! case 0 => "Nada"! case 1 => "One"! case 2 => "A Pair"! case 12 => "a dozen"! case _ if i<0 => "Less that zero"! case _ => "%,d".format(i)}
Sunday, 4 August, 13
![Page 24: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/24.jpg)
Switch by Type
def prettyPrint( a:Any ) = a match {! case i:Int => "%,d".format(i)! case s:String => "[%s]".format(s)! case sym:Symbol => ":%s".format(sym)! case _ => a.toString}
Sunday, 4 August, 13
![Page 25: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/25.jpg)
DecompositionFirst, meet the case class:
Regular class, but with immutable declared fields, toString, equals and hashCode automatically defined
sealed abstract class Treecase class Sum( l:Tree, r:Tree ) extends Treecase class Var( n:String ) extends Treecase class Con( v:Int ) extends Tree
Sunday, 4 August, 13
![Page 26: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/26.jpg)
Decomposition
def evalTree( t:Tree, e:Map[String,Int] ): Int = t match {! case Sum(l,r) => evalTree(l, e) + evalTree(r,e)! case Var( n ) => e(n)! case Con( i ) => i}
Allows downcasting, accessing sub-classes fields and varying actions based on the class of the parameter, in a
single syntactical maneuver
Sunday, 4 August, 13
![Page 27: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/27.jpg)
... In the real world
(merged,original) match {case ( Pass(_), ( _, _) ) => truecase ( Unfixable(_), ( _, _) ) => falsecase ( Fixable(_,t,_), (t1,t2) ) => f.ld<ld(t1)+ld(t2)
}
Given two strings, we need to decide whether it is more likely that they are two separate words or one broken word
Sunday, 4 August, 13
![Page 28: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/28.jpg)
... In the real world
(merged,original) match {case ( Pass(_), ( _, _) ) => truecase ( Unfixable(_), ( _, _) ) => falsecase ( Fixable(_,t,_), (t1,t2) ) => f.ld<ld(t1)+ld(t2)
}
ere are two ways of constructing a software design: One way is to make it so simple that there are obviously no de"ciencies, and the other way is to make it so complicated that there are no obvious de"ciencies. e "rst method is far more difficult.
-- C. A. R. Hoare, Turing Award lecture, 1980
Given two strings, we need to decide whether it is more likely that they are two separate words or one broken word
Sunday, 4 August, 13
![Page 29: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/29.jpg)
Option[T]And the death of the NullPointerException
Indicates possibly missing values
Has two implementations: None and Some(t)
“Collection of at most one item”
Convention more than a language feature
Sunday, 4 August, 13
![Page 30: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/30.jpg)
Functions as valuesCreating new functions from existing ones
Powerful tool, but can get messy
Allows for DSL creation
def bind1( f:(Int,Int)=>Int, v:Int ) =
(a:Int)=>f(a,v)
Sunday, 4 August, 13
![Page 31: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/31.jpg)
Not CoveredCreation of DSLs
Implicit conversions
Continuations
Frameworks
XML
Parsers/Combinators
Macros
Annotations
Genericity
Type System
Actors
Regular Expressions
Extractors
... many more
Sunday, 4 August, 13
![Page 32: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/32.jpg)
Pointers
www.scala-lang.org
Typesafe.com
Scala for the Impatient, by Cay Horstmann (http://horstmann.com/scala/)First chapters available after registering with at Typesafe’s site
(google)
Sunday, 4 August, 13
![Page 33: Invitation to Scala](https://reader033.vdocuments.us/reader033/viewer/2022051608/54003c138d7f7289408b4678/html5/thumbnails/33.jpg)
Introducing ScalaIQSS Tech-Talk, 2013-06-27Michael Bar-Sinai
THANKS
Sunday, 4 August, 13