bootstrapping a scala mindset (scala exchange 2014)
Post on 15-Jul-2015
846 Views
Preview:
TRANSCRIPT
Photo by ColorblindRain - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/11594851@N00 Created with Haiku Deck
Photo by Zach Dischner - Creative Commons Attribution License https://www.flickr.com/photos/35557234@N07 Created with Haiku Deck
Photo by André Hofmeister - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/54786327@N00 Created with Haiku Deck
Photo by Leo Reynolds - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/49968232@N00 Created with Haiku Deck
Photo by Gueоrgui - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/49722723@N00 Created with Haiku Deck
Photo by Infidelic - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/87472505@N00 Created with Haiku Deck
Photo by gruntzooki - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/37996580417@N01 Created with Haiku Deck
Photo by Peter Rivera - Creative Commons Attribution License https://www.flickr.com/photos/13057030@N00 Created with Haiku Deck
Photo by thepartycow - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/44124327211@N01 Created with Haiku Deck
Photo by @Doug88888 - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/29468339@N02 Created with Haiku Deck
Photo by Masa Sakano - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/42548354@N04 Created with Haiku Deck
Photo by Brianellwood1 - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/92848318@N05 Created with Haiku Deck
val v = Vector(1, 2, 3, 4)
v.foreach( print("> " + n) )
// from: Atomic Scala v1.1
def show(n: Int): Unit = {
print("> " + n)
}
val v = Vector(1, 2, 3, 4)
v.foreach(show)
// from: Atomic Scala v1.1
Photo by These * Are * My * Photons - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/61762350@N06 Created with Haiku Deck
// Everything is an expression
// including "statements"
if(1 > 0) {
"It's true!“
} else {
"It’s false!"
}
// adapted from: Atomic Scala v1.1
// 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
// Everything is an expression,
// including “blocks“
val calc =
{
val i1 = 2
val j1 = 4/i1
i1 * j1
}
println(calc)
// from: Atomic Scala v1.1
Photo by plaits - Creative Commons Attribution License https://www.flickr.com/photos/90198349@N04 Created with Haiku Deck
// Infix Notation means that
// this...
a.method(b)
// ... can be written like this...
a method b
// from: Atomic Scala v1.1
// And Infix Notation also means
// that this...
a.method()
// ... can be written like this...
a method
// from: Atomic Scala v1.1
Photo by keoshi - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/46567414@N00 Created with Haiku Deck
class Simple(val s:String) {
def getA() = s
val getB = s + "ya"
}
val simple = new Simple("Hi")
// from: Atomic Scala v1.1
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
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
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
Photo by MTSOfan - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/8628862@N05 Created with Haiku Deck
Photo by Jonohey - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/23465120@N00 Created with Haiku Deck
Photo by Dru! - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/36543076@N00 Created with Haiku Deck
def inc: (i:Int) = i + 1
// this is a TOTAL function
// from: "Programming in Scala
// (2nd Ed.)", pp 332-333
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
import scala.collection.immutable._
val second: List[Int] => Int = {
case x :: y :: _ => y
}
// from: "Programming in Scala
// (2nd Ed.)", pp 332-333
import scala.collection.immutable._
val second:
PartialFunction[List[Int],Int] = {
case x :: y :: _ => y
}
// from: "Programming in Scala
// (2nd Ed.)", pp 332-333
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
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
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
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
Photo by jhull - Creative Commons Attribution License https://www.flickr.com/photos/18937122@N00 Created with Haiku Deck
case class Cat (name:String,
age:Int)
// from: Atomic Scala v1.1
Photo by littlechay - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/59012878@N00 Created with Haiku Deck
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
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
Photo by dharma communications - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/38475709@N00 Created with Haiku Deck
Photo by magnezja - Creative Commons Attribution License https://www.flickr.com/photos/81923938@N02 Created with Haiku Deck
Photo by afagen - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/51035749109@N01 Created with Haiku Deck
Photo by brewbooks - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/93452909@N00 Created with Haiku Deck
Photo by DoNotLick - Creative Commons Attribution License https://www.flickr.com/photos/45972156@N04 Created with Haiku Deck
Photo by Anita363 - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/61897811@N00 Created with Haiku Deck
Photo by kellerabteil - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/7815396@N07 Created with Haiku Deck
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/
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/
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/
Photo by tiswango - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/26395196@N00 Created with Haiku Deck
Photo by Podknox - Creative Commons Attribution License https://www.flickr.com/photos/91198056@N00 Created with Haiku Deck
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
Photo by chriscom - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/31192329@N00 Created with Haiku Deck
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
Photo by taquiman - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/10705232@N06 Created with Haiku Deck
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.)
Photo by Peter Hellberg - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/75489014@N00 Created with Haiku Deck
val result = for {
n <- v // GENERATOR
if n < 10
valueToYield = n
} yield valueToYield
// from: Atomic Scala v.1.1
val result = for {
n <- v
if n < 10 // FILTER
valueToYield = n
} yield valueToYield
// from: Atomic Scala v.1.1
val result = for {
n <- v
if n < 10
valueToYield = n // DEFINITION
} yield valueToYield
// from: Atomic Scala v.1.1
Photo by pico2009 - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/24823485@N06 Created with Haiku Deck
// 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
Photo by td stevenson - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/79471249@N00 Created with Haiku Deck
// 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
// 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
Photo by Deetrak - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/9254684@N05 Created with Haiku Deck
Photo by ilkerender - Creative Commons Attribution License https://www.flickr.com/photos/76805197@N00 Created with Haiku Deck
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
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)
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
Photo by a9r - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/34788578@N08 Created with Haiku Deck
top related