Real-World Functional Programming James Earl Douglas Kelley - - PowerPoint PPT Presentation

real world functional programming
SMART_READER_LITE
LIVE PREVIEW

Real-World Functional Programming James Earl Douglas Kelley - - PowerPoint PPT Presentation

Real-World Functional Programming James Earl Douglas Kelley Robinson @jearldouglas @kelleyrobinson bit.ly/real-world-fp Key Concepts Characteristics that differentiate functional programming Statelessness Immutable data


slide-1
SLIDE 1

Real-World Functional Programming

James Earl Douglas @jearldouglas Kelley Robinson @kelleyrobinson

bit.ly/real-world-fp

slide-2
SLIDE 2

Key Concepts

  • Statelessness
  • Immutable data
  • Referential transparency

Characteristics that differentiate functional programming

slide-3
SLIDE 3

Statelessness

function add(a,b) { return a + b; } var x = add(1,1) // 2 var y = add(1,1) // 2 var z = add(1,1) // 2 add always returns the same output for a given input

slide-4
SLIDE 4

Immutable data

var x = “hello” // “hello” var y = x + “, world” // “hello, world” var z = y.substring(0,5) // “hello”

slide-5
SLIDE 5

Referential transparency

var x = 1

  • x is a synonym for 1

var y = x + 1

  • y is a synonym for x + 1
  • y is also a synonym for 2

x = 2

  • This is a lie, equivalent to 1 = 2
slide-6
SLIDE 6

Benefits

  • Easily abstractable
  • Applications become modular and composable
  • Encourages code reuse

Why you should use functional programming

slide-7
SLIDE 7

We had a problem . . .

slide-8
SLIDE 8

Architectural issues

  • Locked into using one data structure
  • Clumsy persistent data model
  • Bottlenecked deployment

Existing API design had limitations

slide-9
SLIDE 9

Let’s refactor

Using functional concepts to solve our problems.

slide-10
SLIDE 10

Example - ATM

slide-11
SLIDE 11

Withdraw - bad!

slide-12
SLIDE 12

function withdraw(amount) { if (balance >= amount) { balance = balance - amount return amount } else { return 0 } }

Withdraw - bad!

slide-13
SLIDE 13

Withdraw - good!

slide-14
SLIDE 14

function withdraw(amount) { return function(balance) { if (balance >= amount) { return [amount, balance - amount] } else { return [0, balance] } } }

Withdraw - good!

slide-15
SLIDE 15

Check balance

slide-16
SLIDE 16

Generate report

slide-17
SLIDE 17

Generate report

(in Spanish)

slide-18
SLIDE 18

Withdraw - in Bitcoin

slide-19
SLIDE 19

function convertToBtc(withdrawal) { return function(balance) { var result = withdrawal(balance) // [amount, new balance] var inBtc = result[0] / 575.0 var fee = result[0] * 0.01 return [inBtc, result[1] - fee] } } var get20InBtc = convertToBtc(withdraw(20))

Withdraw - in Bitcoin

slide-20
SLIDE 20

Live code time

bit.ly/real-world-fp

slide-21
SLIDE 21

Benefits

  • Easily abstractable
  • Applications become modular and composable
  • Encourages code reuse

Why you should use functional programming

slide-22
SLIDE 22

Thank you!

kelley@versal.com @kelleyrobinson james@versal.com @jearldouglas

bit.ly/real-world-fp