Top Banner
Getting Functional with Scala An Introduction to Functional Programming and the Scala Programming Language September, 2016
21

Getting Functional with Scala

Feb 16, 2017

Download

Software

Jorge Paez
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Getting Functional with Scala

Getting Functional with Scala An Introduction to Functional Programming and the Scala Programming Language

September, 2016

Page 2: Getting Functional with Scala

2

Software Engineer at IBM Mobile Innovation Lab @jorgelpaez19

Jorge Paez

Page 3: Getting Functional with Scala

3

My IBM Journey… So far

www-01.ibm.com/employment/us/extremeblue https://www.ibm.com/innovation/milab/

Page 4: Getting Functional with Scala

4

Where will yours start? http://www-03.ibm.com/employment/entrylevel_campus.html

Page 5: Getting Functional with Scala

5

What is Functional Programming?

Page 6: Getting Functional with Scala

6

Function Not a Function

Page 7: Getting Functional with Scala

7

Core Ideas• Data is immutable

• PURE function are our basic building block • Use expressions over instructions • First class functions • Type-strictness

Page 8: Getting Functional with Scala

8

…but why?

Page 9: Getting Functional with Scala

9

Benefits!• Simpler concurrency/parallelism • Optimizations with caching or memoization • Easier debugging • Cleaner/less verbose code • Encourages code re-use • Promotes Test Driven Development

Page 10: Getting Functional with Scala

10

Pure vs Impure

// In Main.java

public String getFormal(String name) { name = "Mr." + name; return name;}

// In Main.scala

def getFormal(name: String) = s"Mr. $name!"

Page 11: Getting Functional with Scala

11

Expressions vs Instructions

// In Main.java

public double[] convertToMeters(double[] measurements) { double[] metricMeasurements = new double[measurements.length]; for(int i = 0; i < measurements.length; ++i) { metricMeasurements[i] = measurements[i] * CONVERSION_FACTOR; } return metricMeasurements;}

// In Main.scala

def convertToMeters(measurements: Seq[Double]) = measurements.map(n => n * CONVERSION_FACTOR)

Page 12: Getting Functional with Scala

12

First Class Functions

def main(args: Array[String]): Unit = { draw(getCurve(0, 90), 1) draw(getCurve, 2) } def getCurve: Double => Seq[(Double, Double)] = (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] = (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) = ((r * Math.cos(theta)), (r * Math.sin(theta)))def draw(func: Double => Seq[(Double, Double)], scale: Double)

Page 13: Getting Functional with Scala

13

First Class Functions

def main(args: Array[String]): Unit = { draw(getCurve(0, 90), 1) draw(getCurve, 2) } def getCurve: Double => Seq[(Double, Double)] = (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] = (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) = ((r * Math.cos(theta)), (r * Math.sin(theta)))def draw(func: Double => Seq[(Double, Double)], scale: Double)

def getCurve: Double => Seq[(Double, Double)] = (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] = (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))

Page 14: Getting Functional with Scala

14

First Class Functions

def main(args: Array[String]): Unit = { draw(getCurve(0, 90), 1) draw(getCurve, 2) } def getCurve: Double => Seq[(Double, Double)] = (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] = (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) = ((r * Math.cos(theta)), (r * Math.sin(theta)))def draw(func: Double => Seq[(Double, Double)], scale: Double)

def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) = ((r * Math.cos(theta)), (r * Math.sin(theta)))

Page 15: Getting Functional with Scala

15

First Class Functions

def main(args: Array[String]): Unit = { draw(getCurve(0, 90), 1) draw(getCurve, 2) } def getCurve: Double => Seq[(Double, Double)] = (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] = (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) = ((r * Math.cos(theta)), (r * Math.sin(theta)))def draw(func: Double => Seq[(Double, Double)], scale: Double)

def main(args: Array[String]): Unit = { draw(getCurve(0, 90), 1) draw(getCurve, 2) }

def draw(func: Double => Seq[(Double, Double)], scale: Double)

Page 16: Getting Functional with Scala

16

First Class Functions

def main(args: Array[String]): Unit = { draw(getCurve(0, 90), 1) draw(getCurve, 2) } def getCurve: Double => Seq[(Double, Double)] = (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] = (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) = ((r * Math.cos(theta)), (r * Math.sin(theta)))def draw(func: Double => Seq[(Double, Double)], scale: Double)

Page 17: Getting Functional with Scala

17

Type Strictness

sealed trait Personcase class Student(name: String) extends Personcase class Alumni(name: String) extends Persondef getBusFare(person : Person): Int = { person match { case p: Student => 0 case p: Alumni => 2 } }

Page 18: Getting Functional with Scala

18

Options

// Form asking for person's gender// a) Female// b) Male// c) Don’t want to disclosedef main(args: Array[String]): Unit = { val answers = Seq(Some("Female"), Some("Male"), Some(null), None) answers.map(a => a match { case Some(gender) => if(gender == null){ "the didn't want to disclose his or her gender" } else { s"The user's gender is $gender" } case None => "the user didn't pick a value on the form" }) }

Page 19: Getting Functional with Scala

19

What’s next?

Page 20: Getting Functional with Scala

20

Keep Learning• Slides: http://www.slideshare.net/JorgePaez15/getting-functional-with-scala

• Deploying a Scala server to Bluemix: https://www.ibm.com/innovation/milab/how-to-run-a-scala-web-app-on-ibm-bluemix/

• Free class: https://www.coursera.org/learn/progfun1

Page 21: Getting Functional with Scala

Thank You@jorgelpaez19

@IBM_MIL