Dr scalalove: How I learned to stop worrying and love the functions
-
Upload
dianne-marsh -
Category
Technology
-
view
578 -
download
1
description
Transcript of Dr scalalove: How I learned to stop worrying and love the functions
![Page 1: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/1.jpg)
DR. SCALALOVE
Dianne MarshDirector of Engineering -- Cloud Tools, NetflixScalaBay Meetup, Sept 2013
OR HOW
I LEARNED
TO STOP
WORRYING AND
LOVE FUNCTIONS
![Page 2: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/2.jpg)
Dr. Strangelove, 1964
“If this thing turns out to be half as important
as I think it will be, I think you’ll all be in
line for some personal citations and
promotions when this thing is over.”
![Page 3: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/3.jpg)
What is Scala?
Object-oriented + FunctionalRuns on JVMBuild on Java expertise
![Page 4: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/4.jpg)
Why Is it Scary?
Early adopters scared folks awayLost elasticity of learning new programming languagesComfort zone
![Page 5: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/5.jpg)
What’s the Gain?
Modern language featuresLet compiler work for you!
![Page 6: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/6.jpg)
REPL
Interactive InterpreterRead Evaluate Print Loop
![Page 7: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/7.jpg)
Type Inference
val x = 5
val y = "Once upon a time"
val l = List("This", "is", "a", "List")
![Page 8: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/8.jpg)
Methods
def squareIt(x: Int) = x*x
![Page 9: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/9.jpg)
Anatomy of a Case Classcase class Camera(width: Int, height: Int) { def megapixels = (width * height)/1000000.0 def HD = megapixels > 1.08}
val camera = new Camera(1024, 768)camera.megapixels is 0.786432camera.HD is falsecamera is "Camera(1024,768)"
![Page 10: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/10.jpg)
Reducing Boilerplate
class Dimension(var height:Int,
var width:Int)
val c = new Dimension(5,7)
c.height is 5
c.height = 10
c.height is 10
c.width = 19
c.width is 19
![Page 11: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/11.jpg)
Pattern Matching
def batteryIndicator(batteryLevel: Int) =
batteryLevel match {
case l if (l > 60) => "GREEN"
case l if (l > 20) => "YELLOW"
case _ => "RED"
}
![Page 12: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/12.jpg)
Traits
trait Digital {
def batteryIndicator(batteryLevel:
Int) = …}
val d = new Camera(1024, 768) with Digital
d.batteryIndicator(56) is "YELLOW"
![Page 13: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/13.jpg)
Functions are Objects
val v = Vector(19, 1, 7, 3, 2, 14)
v.sorted is Vector(1, 2, 3, 7, 14, 19)
v.sortWith((i, j) => j < i) is
Vector(19, 14, 7, 3, 2, 1)
![Page 14: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/14.jpg)
It’s OK
To use Scala as a Better Java
![Page 15: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/15.jpg)
Reaching into Java
import org.apache.commons.math._import stat.regression.SimpleRegression val r = new SimpleRegressionr.addData(1, 1)r.addData(2, 1.1)r.addData(3, 0.9)r.addData(4, 1.2)r.getN is 4r.predict(6) is 1.19
![Page 16: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/16.jpg)
It’s OK
To be verbose
![Page 17: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/17.jpg)
Verbose Version
// Euler1.scala
val nums = 3 until 1000
val somenums = nums.filter
(x => (x % 3 == 0 || x % 5 ==0))
var sum = 0
somenums.foreach(sum += _)
![Page 18: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/18.jpg)
Find the Sweet Spot
// Euler2.scala
val nums = 3 until 1000
val somenums = nums filter
(x => (x % 3 == 0 || x % 5 ==0))
val sum = somenums reduceLeft
{(x:Int,y:Int) => x + y}
![Page 19: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/19.jpg)
More than 1 way ...
// Euler3.scala
val nums = 3 until 1000
val somenums = nums filter
(x => (x % 3 == 0 || x % 5 ==0))
val sum = somenums.foldLeft(0)(_+_)
![Page 20: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/20.jpg)
Back off if it’s too much
// Euler4.scala
val nums = 3 until 1000
val somenums = nums filter
(x => (x % 3 == 0 || x % 5 ==0))
(0/:somenums)(_+_)
![Page 21: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/21.jpg)
Code should be readable
To your teamTo your customersTo yourself, in 6 months
![Page 22: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/22.jpg)
Killer Apps in Scala
Compute intensiveConcurrency
Reactive applications● Resilient● Scalable● Interactive, single-page● Event Driven
![Page 23: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/23.jpg)
Learning
Scala Koans -- scalakoans.orgCase Studies at TypesafeBlogsBooks
![Page 24: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/24.jpg)
Practicing
Solve Euler problemsUse ScalaTestUse for non-production code
![Page 25: Dr scalalove: How I learned to stop worrying and love the functions](https://reader034.fdocuments.us/reader034/viewer/2022052522/554f93c0b4c9052a518b5518/html5/thumbnails/25.jpg)
Contact Info
We are always hiring ...