40
Joe Barnes Intro to Functional Programming in Scala Senior Software Architect System Design Division October, 2013

Intro to Functional Programming in Scala

  • Upload
    fifi

  • View
    49

  • Download
    0

Embed Size (px)

DESCRIPTION

Intro to Functional Programming in Scala. Joe Barnes. Senior Software Architect System Design Division. October, 2013. Overview. Trends in software influencing the popularity What is different about the paradigm? How do I apply it? More about Scala in particular. Monad tutorial trend. - PowerPoint PPT Presentation

Citation preview

Page 1: Intro to Functional Programming in  Scala

Joe Barnes

Intro to Functional Programming in Scala

Senior Software ArchitectSystem Design Division

October, 2013

Page 2: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential2

Overview

JDB, Intro to Functional Programming in Scala, October 2013

Trends in software influencing the popularity What is different about the paradigm? How do I apply it? More about Scala in particular

Page 3: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential3

Monad tutorial trend

JDB, Intro to Functional Programming in Scala, October 2013

In functional programming, a monad is a structure that represents computations defined as sequences of steps.1

The number of monad tutorials has exploded in the past 10 years.

1Source: Wikipedia | Monad (functional programming) 2Source: Haskell.org | Monad tutorials timeline

2

Page 4: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential4

Moore’s Law

JDB, Intro to Functional Programming in Scala, October 2013

Moore's law is the observation that, over the history of computing hardware, the speed of integrated circuits doubles approximately every two years. 

Moore's law is the observation that, over the history of computing hardware, the number of transistors on integrated circuits doubles approximately every two years. 

In fact, we’re no longer getting faster…

FALSE

Page 5: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential5

Moore’s Law

JDB, Intro to Functional Programming in Scala, October 2013

Source: Herb Sutter | The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software

Page 6: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential6

The free lunch is over!

JDB, Intro to Functional Programming in Scala, October 2013

“ Instead of driving clock speeds and straight-line instruction throughput ever higher, they are instead turning en masse to hyperthreading and multicore architectures”

“Applications will increasingly need to be concurrent if they want to fully exploit continuing exponential CPU throughput gains”

“The vast majority of programmers today don’t grok concurrency, just as the vast majority of programmers [25] years ago didn’t yet grok objects”

Source: Herb Sutter | The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software

Page 7: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential7

As Jimmy McMillan would say…

Your Initials, Presentation Title, Month Year

IS TOO DAMN HARD!!!Source: http://www.troll.me/images/rent-is-too-damn-high/rent-is-too-damn-high.jpg

CONCURRENCY

Page 8: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential8

Why is concurrency hard?

“The programmer must ensure read and write access to objects is properly coordinated (or "synchronized") between threads.”- Java Concurrency | Wikipedia

“C++ has been designed for single thread programming, and parallel programming requires a revolutionary rather than evolutionary change. Two words: data races.”- Bartosz Milewski

JDB, Intro to Functional Programming in Scala, October 2013

Source: Java Concurrency | WikipediaSource: Edward C++Hands | Bartosz Milewski

Page 9: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential9

Data races: The recipe

JDB, Intro to Functional Programming in Scala, October 2013

A data race occurs when two concurrent threads access a shared variable and when at least one access is a write and the threads use no explicit mechanism to prevent

the accesses from being simultaneous.

Restated:1. Two concurrent threads2. At least one write3. Mechanism not used

Source: Eraser: A Dynamic Data Race Detector for Multithreaded Programs | STEFAN SAVAGE, et al

Here to stay

Programmer error. Also here to stay

Hmm…

Page 10: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential10

We Want You

JDB, Intro to Functional Programming in Scala, October 2013

To Stop Using VariablesSource: Some random link via Google image search

Page 11: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential11

Revoke My Writes?!

JDB, Intro to Functional Programming in Scala, October 2013

1968 – Structured Programming— Edsger Dijkstra writes “Go To Statement Considered

Harmful”— Revoked GoTo

1966 – Object Oriented Programming— Ole-Johan Dahl and Kristen Nygaard create Simula 67— Revoked function pointers

1957 – Functional Programming— John McCarthy creates Lisp— Revoked reassignment of variables values

Source: Three Paradigms | Uncle Bob @ 8th Light

Page 12: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential12

Two timelines

JDB, Intro to Functional Programming in Scala, October 2013

Source: Several random links via Google image search

“Higher”

Levels

TheoryTo

Practice

Page 13: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential13

Dichotomies

JDB, Intro to Functional Programming in Scala, October 2013

Von Neumann vs. Lambda Calculus— Manipulation of program state vs. Evaluation of

mathematical formulas

Object-oriented vs. Functional?— False dichotomy per Martin Odersky (BDFL of Scala).— Object-orientation and functional are perpendicular.— Scala is both.

Imperative vs. Functional— The dichotomy defined along the assignment axis.

Page 14: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential14

That’s right. You have no writes.

JDB, Intro to Functional Programming in Scala, October 2013

ALL YOUR WRITES ARE BELONG TO US!

Page 15: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential15

How can we program this way?

JDB, Intro to Functional Programming in Scala, October 2013

What is a function?

Back to school…

Page 16: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential16

Function

JDB, Intro to Functional Programming in Scala, October 2013

Given a set S and a set T, a function f is a set of ordered pairs taken from S ×T where each s from S appears exactly once in f.

Given (s, t) ∈ f, we denote f(s) = t. f is said to “map s to t.”

Functions a.k.a. “mappings” Given s ∈ S, f(s) is a defined value also known as t. Can substitute the expression f(s) with t

— Referential Transparency!

Page 17: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential17

Function (visual)

JDB, Intro to Functional Programming in Scala, October 2013

1

2

3

4

5

A

B

C

D

E

F

f

TS

f = { (1,D), (2,F), (3,A), (4,F), (5,C) }f (1) D, f (2) F, f (3) A, …

Identify any mutable state.

Page 18: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential18

Programming with only functions (Bash)

JDB, Intro to Functional Programming in Scala, October 2013

$ find . -name *.java | xargs grep -l "function" | wc –l

OK, so how do we program with functions?

Notice this script/program doesn’t have mutable state

Page 19: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential19

Programming with only functions (Excel)

JDB, Intro to Functional Programming in Scala, October 2013

Date Station Odometer Gallons Dollars MPG MPD4/23/2012 Sam's 158907 11.098 $40.505/4/2012 Shell 159165 19.742 $75.00 13.07 3.445/15/2012 Sam's 159329 11.407 $39.00 14.38 4.215/25/2012 Shell 159601 17.226 $62.00 15.79 4.396/1/2012 Chevron 159790 12.289 $43.00 15.38 4.46/11/2012 Shell 160146 22.36 $76.00 15.92 4.686/22/2012 Shell 160481 21.521 $71.00 15.57 4.727/10/2012 BP 160807 21.847 $67.51 14.92 4.837/26/2012 BP 161129 20.439 $66.00 15.75 4.888/5/2012 Chevron 161369 18.005 $63.00 13.33 3.818/5/2012 Chevron 161560 10.861 $38.00 17.59 5.038/20/2012 Chevron 161856 20.962 $75.44 14.12 3.928/31/2012 Shell 162122 18.09 $68.00 14.7 3.919/7/2012 Chevron 162282 11.971 $45.00 13.37 3.569/13/2012 Sam's 162432 7.726 $27.50 19.41 5.459/23/2012 Chevron 162611 13.833 $52.00 12.94 3.4410/3/2012 Sam's 162884 18.232 $63.25 14.97 4.3211/2/2012 Shell 163181 19.863 $69.50 14.95 4.2711/19/2012 Sam's 163485 20.389 $64.00 14.91 4.7512/10/2012 Sam's 163834 19.361 $60.00 18.03 5.8212/27/2012 BP 164157 22.272 $71.25 14.5 4.53

fx = DIVIDE(SUM(-C2, C3),D3)

= DIVIDE(SUM(-158907, 159165),19.742)

= DIVIDE(258,19.742)

= 13.07

Page 20: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential20

Referential Transparency

JDB, Intro to Functional Programming in Scala, October 2013

object Math { def roots(a:Double, b:Double, c:Double) = { val discriminant = sqrt(b*b - 4*a*c) val root1 = (-b - discriminant) / (2*a) val root2 = (-b + discriminant) / (2*a) (root1, root2) }}

Find the roots/zeros of the equation ax 2 + bx + c.

Page 21: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential21

Can run in parallel!

Partial ordering of evaluation

JDB, Intro to Functional Programming in Scala, October 2013

(root1, root2)

root1 root2

a b c

discriminant

Page 22: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential22

Yeah, but sometimes we need variables

JDB, Intro to Functional Programming in Scala, October 2013

Produce the first 25 even natural numbers.public class EvensJava { public static List<Integer> first25() { List<Integer> squares = new

ArrayList<Integer>(); for(int i=1; i<=25; i++) squares.add(i*2); }}

FALSE

Page 23: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential23

Squares without variables: Scala

JDB, Intro to Functional Programming in Scala, October 2013

object EvensScala extends App { val first25 = (1 to 25).map { i => i*2 }}

object EvensScala extends App { val first25 = (1 to 25).map(_*2)}

object EvensScala extends App { val first25 = (1 to 25).par.map(_*2)}

Page 24: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential24

Calculate factorial: Java

JDB, Intro to Functional Programming in Scala, October 2013

public class FactorialJava { public int looped(int n) { if(n < 0) throw new IllegalArgumentException("n < 0"); int factorial = 1; for(int i=1; i<=n; i++) factorial *= i; return factorial; }

public int recursive(final int n) { if(n < 0) throw new IllegalArgumentException("n < 0"); if(n <= 1) return 1; else return n * recursive(n-1); }}

Page 25: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential25

Calculate factorial: Scala

JDB, Intro to Functional Programming in Scala, October 2013

class FactorialScala { def recursive(n:Int):Int = { require(n >= 0, "n < 0") if(n <= 1) 1 else n * recursive(n-1) }

def reduced(n:Int):Int = { require(n >= 0, "n < 0") (1 to n).reduceLeftOption(_ * _).getOrElse(1) }}

Page 26: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential26

First n primes: Java

JDB, Intro to Functional Programming in Scala, October 2013

public class PrimesJava {

public List<Integer> first(int n) {

List<Integer> primes = new LinkedList<Integer>();

for(int i=2; primes.size() < n; i++) {

if(isPrime(i)) primes.add(i);

}

return primes;

}

public boolean isPrime(int n) {

boolean isPrime = true;

for(int i=2; i<n; i++) {

if(n % i == 0) {

isPrime = false;

break;

}

}

return isPrime;

}

}

Page 27: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential27

First n primes: Scala

JDB, Intro to Functional Programming in Scala, October 2013

class PrimesScala { def isPrime(n:Int) = (2 to (n-1)).forall { i => n % i != 0 } def first(n:Int) = Stream.from(2). filter(isPrime(_)).take(n)}

Page 28: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential28

Persistent data structures

JDB, Intro to Functional Programming in Scala, October 2013

class Prepend extends FunSuite { test("Prepend produces new instance") { val oneTo3 = List(1, 2, 3) val zeroTo3 = 0 +: oneTo3 assert(oneTo3.size == 3) assert(zeroTo3.size == 4) assert(oneTo3 != zeroTo3) }}

Page 29: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential29

But isn’t that inefficient??

JDB, Intro to Functional Programming in Scala, October 2013

val oneTo3 = List(1, 2, 3)

val zeroTo3 = 0 +: oneTo3

1 2 3

oneTo3

0

zeroTo3

Page 30: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential30

So why Scala?

JDB, Intro to Functional Programming in Scala, October 2013

Compiles to plain-ol’ JVM byte code— Fully interoperable with Java— Leverage solid existing Java technologies— Platform-independent

Multi-paradigm— Arguably more OO than Java

– No primitives– No static– Multiple inheritance– Even functions are objects!

— Idiomatically functional, but not strictly– Can declare a var

— Be productively quickly, learn new paradigm gradually

Page 31: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential31

So why Scala, continued…

JDB, Intro to Functional Programming in Scala, October 2013

Statically-typed with dynamically-typed syntax features— Richer type system than Java (Turing complete)— Type inference— Implicit conversions— Pattern matching— Malleable syntax allows domain-specific languages (DSL)

All of the aforementioned advantages of functional programming— Multi-processor scaling

High-productivity— Dynamically-typed syntax features— Typically takes one half to one third of the lines of code as

Java1

1Source: Research: Programming Style and Productivity | scala-lang.org

Page 32: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential32

Yay! The world’s problems solved!

JDB, Intro to Functional Programming in Scala, October 2013

No backwards compatibility across major releases— Ex. Scala 2.9.x byte code incompatible with 2.10.x byte

code— Have to find and possibly recompile libraries to advance— Deprecated features get obsoleted

Compiler slower than Java— Does MUCH more for the developer than Java (see type

system)— Defacto build tool (sbt) keeps compiler in memory which

helps Less mature

— Tooling isn’t as stable

Page 33: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential33

Java/Scala timeline

JDB, Intro to Functional Programming in Scala, October 2013

1995 – Java 1.0 released by Sun. 1995 – Odersky begins work on OO/FP language

targeting the JVM. Efforts eventually lead to Java 1.5’s generics.

2001 – Odersky begins Scala from scratch due to restrictions imposed by Java.

2003 – First release of Scala 2004 – Java 5.0 released 2006 – Scala 2 released 2011 – Typesafe launched to provide commercial

support, training, services for Scala. 2011 – Java 7 released; JVM includes

InvokeDynamic.Source: A Brief History of Scala | Martin OderskySource: Scala (programming language) | Wikipedia

Page 34: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential34

Scala sounds nice, but does it work?

JDB, Intro to Functional Programming in Scala, October 2013

Twitter used Scala to kill the fail whale!

Source: The Secret Behind Twitter’s Growth | MIT Technology Review

Page 35: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential35 JDB, Intro to Functional Programming in Scala, October 2013

Source: Sneaking Scala Through the Back Door | Dianne Marsh

Adoption

? ??

? ????

Page 36: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential36

Scala at Mentor Graphics?

JDB, Intro to Functional Programming in Scala, October 2013

Scala is NOT hereby endorsed by Mentor Graphics or System Design Division for use in production code.

Adoption of Scala in production code is a division-level decision.

Page 37: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential37

Where might Scala fit, then?

JDB, Intro to Functional Programming in Scala, October 2013

Find problems where Scala is a great fit— Web applications

– Application state belongs in the DB. — Static typing

– Consumption of generated code, such as SOAP calls– Slow-running I/O-bound processes, such as building– Anywhere compiler checking can detect changes in

dependencies— Domain-specific languages

– Any time you want code to read fluidly

Utilize Scala in non-production/controlled environments — Hosted web applications— Internal applications— Testing

Page 38: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential38

Testing DSL example

JDB, Intro to Functional Programming in Scala, October 2013

"The dashboard" should { "filter journal when searching" in { search "Knees to elbows" eventually { entries.size should be (1) } Entry(1).title should be (workout2.title) click on Entry(1).resultBtn eventually { Entry(1).notes should be ("I completed the workout!") } }}

Page 39: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential39

Taking the next step

JDB, Intro to Functional Programming in Scala, October 2013

Free online course!— Functional Programming Principles in Scala— Taught by Martin Odersky himself

Join the Scala User Group Join the Scala Enthusiasts group on LinkedIn Reference the Scala API Ask questions at StackOverflow Email me [email protected]

— User group? Read my Scala ramblings

Image: Kool Aid Guy Wreaks Havoc at Presbyterian Seminary

Page 40: Intro to Functional Programming in  Scala

www.mentor.com© 2013 Mentor Graphics Corp. Company Confidential

QUESTIONS?