DR.
SCALA
LOVE
Dianne Marsh
Director of Engineering -- Cloud Tools, Netflix
ScalaBay Meetup, Sept 2013
OR
HOW
I
LEARNED
TO...
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
lin...
What is Scala?
Object-oriented + Functional
Runs on JVM
Build on Java expertise
Why Is it Scary?
Early adopters scared folks away
Lost elasticity of learning new programming
languages
Comfort zone
What’s the Gain?
Modern language features
Let compiler work for you!
REPL
Interactive Interpreter
Read Evaluate Print Loop
Type Inference
val x = 5
val y = "Once upon a time"
val l = List("This", "is", "a",
"List")
Methods
def squareIt(x: Int) = x*x
Anatomy of a Case Class
case class Camera(width: Int,
height: Int) {
def megapixels = (width * height)
/1000000.0
def HD =...
Reducing Boilerplate
class Dimension(var height:Int,
var width:Int)
val c = new Dimension(5,7)
c.height is 5
c.height = 10...
Pattern Matching
def batteryIndicator(batteryLevel:
Int) =
batteryLevel match {
case l if (l > 60) => "GREEN"
case l if (l...
Traits
trait Digital {
def batteryIndicator(batteryLevel:
Int) = …
}
val d = new Camera(1024, 768) with
Digital
d.batteryI...
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...
It’s OK
To use Scala as a Better Java
Reaching into Java
import org.apache.commons.math._
import stat.regression.SimpleRegression
val r = new SimpleRegression
r...
It’s OK
To be verbose
Verbose Version
// Euler1.scala
val nums = 3 until 1000
val somenums = nums.filter
(x => (x % 3 == 0 || x % 5 ==0))
var su...
Find the Sweet Spot
// Euler2.scala
val nums = 3 until 1000
val somenums = nums filter
(x => (x % 3 == 0 || x % 5 ==0))
va...
More than 1 way ...
// Euler3.scala
val nums = 3 until 1000
val somenums = nums filter
(x => (x % 3 == 0 || x % 5 ==0))
va...
Back off if it’s too much
// Euler4.scala
val nums = 3 until 1000
val somenums = nums filter
(x => (x % 3 == 0 || x % 5 ==...
Code should be readable
To your team
To your customers
To yourself, in 6 months
Killer Apps in Scala
Compute intensive
Concurrency
Reactive applications
● Resilient
● Scalable
● Interactive, single-page...
Learning
Scala Koans -- scalakoans.org
Case Studies at Typesafe
Blogs
Books
Practicing
Solve Euler problems
Use ScalaTest
Use for non-production code
Contact Info
@dmarsh
dmarsh@netflix.com
We are always hiring ...
Upcoming SlideShare
Loading in …5
×

Dr scalalove: How I learned to stop worrying and love the functions

913 views

Published on

n this introduction to Scala, I will demonstrate why I love Scala and why I think that you will too. You’ll learn why you don’t need to go from 0 to functions and how you can ease yourself into this language. I’ll show you some Scala, compare it to Java, and give you pointers on how to start your journey into this functional object-oriented language.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
913
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Dr scalalove: How I learned to stop worrying and love the functions

  1. 1. DR. SCALA LOVE Dianne Marsh Director of Engineering -- Cloud Tools, Netflix ScalaBay Meetup, Sept 2013 OR HOW I LEARNED TO STOP WORRYING AND LOVE FUNCTIONS
  2. 2. 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.”
  3. 3. What is Scala? Object-oriented + Functional Runs on JVM Build on Java expertise
  4. 4. Why Is it Scary? Early adopters scared folks away Lost elasticity of learning new programming languages Comfort zone
  5. 5. What’s the Gain? Modern language features Let compiler work for you!
  6. 6. REPL Interactive Interpreter Read Evaluate Print Loop
  7. 7. Type Inference val x = 5 val y = "Once upon a time" val l = List("This", "is", "a", "List")
  8. 8. Methods def squareIt(x: Int) = x*x
  9. 9. Anatomy of a Case Class case 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.786432 camera.HD is false camera is "Camera(1024,768)"
  10. 10. 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
  11. 11. Pattern Matching def batteryIndicator(batteryLevel: Int) = batteryLevel match { case l if (l > 60) => "GREEN" case l if (l > 20) => "YELLOW" case _ => "RED" }
  12. 12. Traits trait Digital { def batteryIndicator(batteryLevel: Int) = … } val d = new Camera(1024, 768) with Digital d.batteryIndicator(56) is "YELLOW"
  13. 13. 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)
  14. 14. It’s OK To use Scala as a Better Java
  15. 15. Reaching into Java import org.apache.commons.math._ import stat.regression.SimpleRegression val r = new SimpleRegression r.addData(1, 1) r.addData(2, 1.1) r.addData(3, 0.9) r.addData(4, 1.2) r.getN is 4 r.predict(6) is 1.19
  16. 16. It’s OK To be verbose
  17. 17. 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 += _)
  18. 18. 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}
  19. 19. 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)(_+_)
  20. 20. 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)(_+_)
  21. 21. Code should be readable To your team To your customers To yourself, in 6 months
  22. 22. Killer Apps in Scala Compute intensive Concurrency Reactive applications ● Resilient ● Scalable ● Interactive, single-page ● Event Driven
  23. 23. Learning Scala Koans -- scalakoans.org Case Studies at Typesafe Blogs Books
  24. 24. Practicing Solve Euler problems Use ScalaTest Use for non-production code
  25. 25. Contact Info @dmarsh dmarsh@netflix.com We are always hiring ...

×