bootstrapping a scala mindset (scala exchange 2014)

79
Photo by ColorblindRain - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/11594851@N00 Created with Haiku Deck

Upload: andrew-harmel-law

Post on 15-Jul-2015

830 views

Category:

Software


6 download

TRANSCRIPT

Page 1: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by ColorblindRain - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/11594851@N00 Created with Haiku Deck

Page 2: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Zach Dischner - Creative Commons Attribution License https://www.flickr.com/photos/35557234@N07 Created with Haiku Deck

Page 3: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by André Hofmeister - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/54786327@N00 Created with Haiku Deck

Page 4: Bootstrapping a Scala Mindset (Scala eXchange 2014)
Page 5: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Leo Reynolds - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/49968232@N00 Created with Haiku Deck

Page 6: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Gueоrgui - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/49722723@N00 Created with Haiku Deck

Page 7: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Infidelic - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/87472505@N00 Created with Haiku Deck

Page 8: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by gruntzooki - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/37996580417@N01 Created with Haiku Deck

Page 9: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Peter Rivera - Creative Commons Attribution License https://www.flickr.com/photos/13057030@N00 Created with Haiku Deck

Page 10: Bootstrapping a Scala Mindset (Scala eXchange 2014)
Page 11: Bootstrapping a Scala Mindset (Scala eXchange 2014)
Page 12: Bootstrapping a Scala Mindset (Scala eXchange 2014)
Page 13: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by thepartycow - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/44124327211@N01 Created with Haiku Deck

Page 14: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by @Doug88888 - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/29468339@N02 Created with Haiku Deck

Page 15: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Masa Sakano - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/42548354@N04 Created with Haiku Deck

Page 16: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Brianellwood1 - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/92848318@N05 Created with Haiku Deck

Page 17: Bootstrapping a Scala Mindset (Scala eXchange 2014)

val v = Vector(1, 2, 3, 4)

v.foreach( print("> " + n) )

// from: Atomic Scala v1.1

Page 18: Bootstrapping a Scala Mindset (Scala eXchange 2014)

def show(n: Int): Unit = {

print("> " + n)

}

val v = Vector(1, 2, 3, 4)

v.foreach(show)

// from: Atomic Scala v1.1

Page 19: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by These * Are * My * Photons - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/61762350@N06 Created with Haiku Deck

Page 20: Bootstrapping a Scala Mindset (Scala eXchange 2014)

// Everything is an expression

// including "statements"

if(1 > 0) {

"It's true!“

} else {

"It’s false!"

}

// adapted from: Atomic Scala v1.1

Page 21: Bootstrapping a Scala Mindset (Scala eXchange 2014)

// Everything is an expression

// including "statements"

val result =

if(1 > 0) {

"It's true!"

} else {

"It's false!"

}

println(result) // “It’s true!”

// adapted from: Atomic Scala v1.1

Page 22: Bootstrapping a Scala Mindset (Scala eXchange 2014)

// Everything is an expression,

// including “blocks“

val calc =

{

val i1 = 2

val j1 = 4/i1

i1 * j1

}

println(calc)

// from: Atomic Scala v1.1

Page 23: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by plaits - Creative Commons Attribution License https://www.flickr.com/photos/90198349@N04 Created with Haiku Deck

Page 24: Bootstrapping a Scala Mindset (Scala eXchange 2014)

// Infix Notation means that

// this...

a.method(b)

// ... can be written like this...

a method b

// from: Atomic Scala v1.1

Page 25: Bootstrapping a Scala Mindset (Scala eXchange 2014)

// And Infix Notation also means

// that this...

a.method()

// ... can be written like this...

a method

// from: Atomic Scala v1.1

Page 26: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by keoshi - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/46567414@N00 Created with Haiku Deck

Page 27: Bootstrapping a Scala Mindset (Scala eXchange 2014)

class Simple(val s:String) {

def getA() = s

val getB = s + "ya"

}

val simple = new Simple("Hi")

// from: Atomic Scala v1.1

Page 28: Bootstrapping a Scala Mindset (Scala eXchange 2014)

class Simple(val s:String) {

def getA() = s

val getB = s + "ya"

}

val simple = new Simple("Hi")

simple.getA() // res0: String = Hi

// from: Atomic Scala v1.1

Page 29: Bootstrapping a Scala Mindset (Scala eXchange 2014)

class Simple(val s:String) {

def getA() = s

val getB = s + "ya"

}

val simple = new Simple("Hi")

simple getA // res4: String = Hi

// from: Atomic Scala v1.1

Page 30: Bootstrapping a Scala Mindset (Scala eXchange 2014)

class Simple(val s:String) {

def getA() = s

val getB = s + "ya"

}

val simple = new Simple("Hi")

simple getB // res5: String = Hiya

// from: Atomic Scala v1.1

Page 31: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by MTSOfan - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/8628862@N05 Created with Haiku Deck

Page 32: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Jonohey - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/23465120@N00 Created with Haiku Deck

Page 33: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Dru! - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/36543076@N00 Created with Haiku Deck

Page 34: Bootstrapping a Scala Mindset (Scala eXchange 2014)

def inc: (i:Int) = i + 1

// this is a TOTAL function

// from: "Programming in Scala

// (2nd Ed.)", pp 332-333

Page 35: Bootstrapping a Scala Mindset (Scala eXchange 2014)

def inc (i:Int) = i + 1

// this is a TOTAL function

def fraction (d:Int) = 42 / d

// this is a PARTIAL function

// It is not defined for d == 0

// E.g. It’ll throw an exception

// if called with 0

// from: "Programming in Scala

// (2nd Ed.)", pp 332-333

Page 36: Bootstrapping a Scala Mindset (Scala eXchange 2014)

import scala.collection.immutable._

val second: List[Int] => Int = {

case x :: y :: _ => y

}

// from: "Programming in Scala

// (2nd Ed.)", pp 332-333

Page 37: Bootstrapping a Scala Mindset (Scala eXchange 2014)

import scala.collection.immutable._

val second:

PartialFunction[List[Int],Int] = {

case x :: y :: _ => y

}

// from: "Programming in Scala

// (2nd Ed.)", pp 332-333

Page 38: Bootstrapping a Scala Mindset (Scala eXchange 2014)

import scala.collection.immutable._

val second:

PartialFunction[List[Int],Int] = {

case x :: y :: _ => y

}

second.isDefinedAt(List(5,6,7))

res30: Boolean = true

// from: "Programming in Scala

// (2nd Ed.)", pp 332-333

Page 39: Bootstrapping a Scala Mindset (Scala eXchange 2014)

import scala.collection.immutable._

val second:

PartialFunction[List[Int],Int] = {

case x :: y :: _ => y

}

second.isDefinedAt(List())

res30: Boolean = false

// from: "Programming in Scala

// (2nd Ed.)", pp 332-333

Page 40: Bootstrapping a Scala Mindset (Scala eXchange 2014)

class Problem(val msg:String)

extends Exception

def test(n:Int) =

try {

f(n)

} catch {

case err:Problem

=> s"Failed: ${err.msg}"

}

// from: Atomic Scala v1.1

Page 41: Bootstrapping a Scala Mindset (Scala eXchange 2014)

class Problem(val msg:String)

extends Exception

def test(n:Int) =

try {

f(n)

} catch {

case err:Problem => s“whoops“

case err:Disaster => s“BOOM!“

}

// from: Atomic Scala v1.1

Page 42: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by jhull - Creative Commons Attribution License https://www.flickr.com/photos/18937122@N00 Created with Haiku Deck

Page 43: Bootstrapping a Scala Mindset (Scala eXchange 2014)

case class Cat (name:String,

age:Int)

// from: Atomic Scala v1.1

Page 44: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by littlechay - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/59012878@N00 Created with Haiku Deck

Page 45: Bootstrapping a Scala Mindset (Scala eXchange 2014)

def acceptAnything(x:Any):String = {

x match {

case "Exactly" => "An exact match!"

case s:String => "A String: " + s

case i:Int if (i < 20)

=> s"Int < 20: $i"

case p:Person => s"A person ${p.name}"

case _ => "I don't know what!"

}

}

// from: Atomic Scala v1.1

Page 46: Bootstrapping a Scala Mindset (Scala eXchange 2014)

def findCatsCalledBob(x:Cat):String = {

x match {

case Cat("Bob", age)

=> s"Got $age y/o cat called Bob"

case _ => "I don't know what"

}

}

// from: My own Brain

Page 47: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by dharma communications - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/38475709@N00 Created with Haiku Deck

Page 48: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by magnezja - Creative Commons Attribution License https://www.flickr.com/photos/81923938@N02 Created with Haiku Deck

Page 49: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by afagen - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/51035749109@N01 Created with Haiku Deck

Page 50: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by brewbooks - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/93452909@N00 Created with Haiku Deck

Page 51: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by DoNotLick - Creative Commons Attribution License https://www.flickr.com/photos/45972156@N04 Created with Haiku Deck

Page 52: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Anita363 - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/61897811@N00 Created with Haiku Deck

Page 53: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by kellerabteil - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/7815396@N07 Created with Haiku Deck

Page 54: Bootstrapping a Scala Mindset (Scala eXchange 2014)

val maybeFirstName : Option[String] =

Some("Mark")

val maybeSurname : Option[String] = None

// from: http://www.markhneedham.com/blog/2011/09/15/scala-for-

// comprehensions-with-options/

Page 55: Bootstrapping a Scala Mindset (Scala eXchange 2014)

case class Person(firstName:String,

surname:String)

val maybeFirstName : Option[String] =

Some("Mark")

val maybeSurname : Option[String] = None

for { firstName <- maybeFirstName;

surname <- maybeSurname }

yield Person(firstName, surname)

// res01: Option[Person] = None

// from: http://www.markhneedham.com/blog/2011/09/15/scala-for-

// comprehensions-with-options/

Page 56: Bootstrapping a Scala Mindset (Scala eXchange 2014)

case class Person(firstName:String,

surname:String)

val maybeFirstName : Option[String] =

Some("Mark")

val maybeSurname : Option[String] =

Some("Needham")

for { firstName <- maybeFirstName;

surname <- maybeSurname }

yield Person(firstName, surname)

// res01: Option[Person] =

Some(Person(Mark,Needham)

// from: http://www.markhneedham.com/blog/2011/09/15/scala-for-

// comprehensions-with-options/

Page 57: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by tiswango - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/26395196@N00 Created with Haiku Deck

Page 58: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Podknox - Creative Commons Attribution License https://www.flickr.com/photos/91198056@N00 Created with Haiku Deck

Page 59: Bootstrapping a Scala Mindset (Scala eXchange 2014)

val maybeFirstName : Option[String] =

Some("me")

val maybeSurname : Option[String] = None

val result = maybeFirstName.map(_.toUpperCase)

// result : Option[String] = Some(ME)

val result = maybeSurame.map(_.toUpperCase)

// result: Option[String] = None

// from: http://alvinalexander.com/

// scala/collection-scala-flatmap-examples-

// map-flatten

Page 60: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by chriscom - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/31192329@N00 Created with Haiku Deck

Page 61: Bootstrapping a Scala Mindset (Scala eXchange 2014)

val strings = Seq("1", "2", "foo", "3", "bar")

strings: Seq[java.lang.String]

= List(1, 2, foo, 3, bar)

strings.map(toInt)

res0: Seq[Option[Int]] = List(Some(1),

Some(2), None, Some(3), None)

strings.flatMap(toInt)

res1: Seq[Int] = List(1, 2, 3)

// from: http://alvinalexander.com/

// scala/collection-scala-flatmap-examples-

// map-flatten

Page 62: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by taquiman - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/10705232@N06 Created with Haiku Deck

Page 63: Bootstrapping a Scala Mindset (Scala eXchange 2014)

case class Person(name: String,

isMale : Boolean, children: Person*)

val lara = Person("Lara", false)

val bob = Person("Bob", true)

val julie = Person("Julie", false, lara, bob)

val persons = List(lara, bob, julie)

persons filter (p => !p.isMale)

res0: List[worksheet2.Person] =

List(Person(Lara,false,WrappedArray()),

Person(Julie,false,WrappedArray(...)))

// from: Programming in Scala (2nd Ed.)

Page 64: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Peter Hellberg - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/75489014@N00 Created with Haiku Deck

Page 65: Bootstrapping a Scala Mindset (Scala eXchange 2014)

val result = for {

n <- v // GENERATOR

if n < 10

valueToYield = n

} yield valueToYield

// from: Atomic Scala v.1.1

Page 66: Bootstrapping a Scala Mindset (Scala eXchange 2014)

val result = for {

n <- v

if n < 10 // FILTER

valueToYield = n

} yield valueToYield

// from: Atomic Scala v.1.1

Page 67: Bootstrapping a Scala Mindset (Scala eXchange 2014)

val result = for {

n <- v

if n < 10

valueToYield = n // DEFINITION

} yield valueToYield

// from: Atomic Scala v.1.1

Page 68: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by pico2009 - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/24823485@N06 Created with Haiku Deck

Page 69: Bootstrapping a Scala Mindset (Scala eXchange 2014)

// Single Generator plus filter?

val v = Vector(1,2,3,5,6,7,8,10,13,14,17)

val result = for {

n <- v

if n % 2 == 0

} yield n * 2

// Desugars to:

val resultMap =

v.withFilter(n => n % 2 == 0)

.map(n => n * 2)

// res1: Vector(4,12,16,20,28)

// from: My Own Brain

Page 70: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by td stevenson - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/79471249@N00 Created with Haiku Deck

Page 71: Bootstrapping a Scala Mindset (Scala eXchange 2014)

// Multiple Generators

val v = Vector(1,2,3,5,6,7,8,10,13,14,17)

val w = Vector(1)

val result = for {

x <- v

y <- w

} yield x == y

// Desugars to:

val resultMap2 =

v.flatMap{ x => w.map{ y => x == y } }

// res2: Vector(true, false, false, false,…)

// from: My Own Brain

Page 72: Bootstrapping a Scala Mindset (Scala eXchange 2014)

// Multiple Generators

val v = Vector(1,2,3,5,6,7,8,10,13,14,17)

val w = Vector(1)

val result = for {

x <- v

y <- w

} yield x == y

// Desugars to:

val resultMap2 =

v.map{ x => w.map{ y => x == y } }

// res2: Vector(Vector(true), Vector(false),…)

// from: My Own Brain

Page 73: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Deetrak - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/9254684@N05 Created with Haiku Deck

Page 74: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by ilkerender - Creative Commons Attribution License https://www.flickr.com/photos/76805197@N00 Created with Haiku Deck

Page 75: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by twiga269 ॐ FEMEN - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/24257706@N07 Created with Haiku Deck

Tips:

• Read closely. (Slowly)

• Beware your Java-sense

• But don’t ignore it

• Unlearning is HARD,

and it HURTS

Page 76: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by ecstaticist - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/41864721@N00 Created with Haiku Deck

• Atomic Scala (Eckel & Marsh)

• The Scala Koans

• Scala for the Impatient (Horstmann)

• Twitter’s Scala School

• Introduction to Functional Programming

(Coursera)

• Programming in Scala (2nd Ed.) (Odersky,

Venners, Spoon)

• The Scala Language Specification

(Seriously)

Page 77: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by Jack Lazar - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/38638219@N04 Created with Haiku Deck

• Implicits

• Type Classes

• Learn you a Haskell

• Revisit

Page 78: Bootstrapping a Scala Mindset (Scala eXchange 2014)
Page 79: Bootstrapping a Scala Mindset (Scala eXchange 2014)

Photo by a9r - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/34788578@N08 Created with Haiku Deck