Slide 1

Slide 1 text

THINKING FUNCTIONALLY IN RUBY. @tomstuart

Slide 2

Slide 2 text

Functional programming is a pretty neat idea. 1:

Slide 3

Slide 3 text

Enumerable contains some useful methods. 2:

Slide 4

Slide 4 text

I hope these things are connected.

Slide 5

Slide 5 text

(Functional programming is a pretty neat idea) 1.

Slide 6

Slide 6 text

What is functional programming?

Slide 7

Slide 7 text

A programming style.

Slide 8

Slide 8 text

Two broad families of languages:

Slide 9

Slide 9 text

1. Lisp-like •Scheme •Common Lisp •Dylan •Clojure •Dynamically typed •Homoiconic (code is data) •Lists are fundamental

Slide 10

Slide 10 text

2. ML-like •Standard ML •Haskell •OCaml •Scala (sort of) •Statically typed (+ reconstruction) •Code is not data •ADTs and pattern matching

Slide 11

Slide 11 text

Functions as values

Slide 12

Slide 12 text

What is a function?

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

1 7 2 3 6 4 5 4 2 6 8 3 7 1

Slide 17

Slide 17 text

Functions as values

Slide 18

Slide 18 text

“First-class functions” Implies: higher-order functions “closures”, “lambdas”, “anonymous functions”

Slide 19

Slide 19 text

No side effects

Slide 20

Slide 20 text

Values are immutable All functions do is return their result No state No I/O

Slide 21

Slide 21 text

(In reality, different languages accomplish this to varying degrees.)

Slide 22

Slide 22 text

Implies: recursion Implies: persistent data structures

Slide 23

Slide 23 text

So what?

Slide 24

Slide 24 text

Unfortunately...

Slide 25

Slide 25 text

Declarative programming is counterintuitive when youʼre accustomed to imperative programming WHAT! HOW!

Slide 26

Slide 26 text

Copying is expensive

Slide 27

Slide 27 text

But!

Slide 28

Slide 28 text

Referential transparency “an expression can be replaced with its value” • Good for efficiency • caching/memoisation/inlining • ultimately more efficient • Good for programmer understanding • state is incredibly hard to deal with • ultimately more intuitive • Good for code reuse and testing

Slide 29

Slide 29 text

Highly expressive Deeply satisfying Strongly compositional

Slide 30

Slide 30 text

Future-proof •Mooreʼs Law is running out of steam •similar transistor density, more cores •The futureʼs concurrent •Concurrent access to mutable state is hard... •but not if you donʼt have mutable state! •Parallelising an algorithm is hard... •but itʼs easier if your code isnʼt overspecified

Slide 31

Slide 31 text

OK seriously: so what?

Slide 32

Slide 32 text

Ruby can do some of this. Not really a functional language, but we can pretend and get some of the benefits.

Slide 33

Slide 33 text

Use function values lambda { |x| x + 1 } blocks, procs

Slide 34

Slide 34 text

Consider treating your values as immutable State is rarely worth it. It will kill you in the end.

Slide 35

Slide 35 text

http://clojure.org/state

Slide 36

Slide 36 text

Use the functional-flavoured parts of the standard library

Slide 37

Slide 37 text

Be declarative. Think functionally. What, not how.

Slide 38

Slide 38 text

2. (Enumerable contains some useful methods)

Slide 39

Slide 39 text

Enumerable#zip

Slide 40

Slide 40 text

[1, 2, 3, 4]. zip([5, 6, 7, 8]) [1, 2, 3, 4]. zip([5, 6, 7, 8])

Slide 41

Slide 41 text

2 1 3 4 6 5 7 8

Slide 42

Slide 42 text

2 1 3 4 6 5 7 8

Slide 43

Slide 43 text

[ ] 2 1 3 4 6 5 7 8 [ ] , , , , [ ] [ ] [ ] , , ,

Slide 44

Slide 44 text

[1, 2, 3, 4]. zip([5, 6, 7, 8], [9, 10, 11, 12])

Slide 45

Slide 45 text

Enumerable#select (a.k.a. #find_all)

Slide 46

Slide 46 text

{ |x| x.odd? }

Slide 47

Slide 47 text

1 1 ?

Slide 48

Slide 48 text

1 1 ? !

Slide 49

Slide 49 text

1 1 ? ! 2 2 ?

Slide 50

Slide 50 text

1 1 ? ! 2 2 ? "

Slide 51

Slide 51 text

[1, 2, 3, 4]. select { |x| x.odd? }

Slide 52

Slide 52 text

1 2 3 4 2 1 3 4 ? ? ? ?

Slide 53

Slide 53 text

1 2 3 4 2 1 3 4 ? ? ? ? ! " " !

Slide 54

Slide 54 text

1 3 1 3 ? ? ! ! 1 3

Slide 55

Slide 55 text

1 3 [ ] ,

Slide 56

Slide 56 text

Enumerable#partition

Slide 57

Slide 57 text

[1, 2, 3, 4]. partition { |x| x.odd? }

Slide 58

Slide 58 text

1 2 3 4 2 1 3 4 ? ? ? ?

Slide 59

Slide 59 text

1 2 3 4 2 1 3 4 ? ? ? ? ! " " !

Slide 60

Slide 60 text

1 ? ! 4 ? " 2 ? " 3 ? !

Slide 61

Slide 61 text

1 ? ! 4 ? " 2 ? " 3 ? ! 1 3 2 4

Slide 62

Slide 62 text

1 3 [ ] , 2 4 [ ] , , [ ]

Slide 63

Slide 63 text

Enumerable#map (a.k.a. #collect)

Slide 64

Slide 64 text

{ |x| x * 3 }

Slide 65

Slide 65 text

2 2 6 ×3

Slide 66

Slide 66 text

2 2 6 ×3

Slide 67

Slide 67 text

[1, 2, 3, 4]. map { |x| x * 3 }

Slide 68

Slide 68 text

1 2 3 4 2 1 3 4 6 3 9 12 ×3 ×3 ×3 ×3

Slide 69

Slide 69 text

1 2 3 4 2 1 3 4 6 3 9 12 ×3 ×3 ×3 ×3

Slide 70

Slide 70 text

6 3 9 12 [ ] , , ,

Slide 71

Slide 71 text

Enumerable#inject (a.k.a. #reduce)

Slide 72

Slide 72 text

{ |x, y| x + y }

Slide 73

Slide 73 text

3 3 5 5 8 +

Slide 74

Slide 74 text

3 3 5 5 8 +

Slide 75

Slide 75 text

[1, 2, 3, 4]. inject(0) { |x,y| x+y } [1, 2, 3, 4]. inject(0) { |x,y| x+y }

Slide 76

Slide 76 text

1 2 3 4 1 2 3 4 0 1 3 6 10 + + + +

Slide 77

Slide 77 text

1 1 2 3 3 6 4 0 1 2 3 4 0 1 3 6 10 + + + +

Slide 78

Slide 78 text

module Enumerable def inject(initial) result = initial for element in self result = yield(result, element) end result end end

Slide 79

Slide 79 text

a.k.a. “left fold” (foldl, fold_left)

Slide 80

Slide 80 text

1 2 3 4 0

Slide 81

Slide 81 text

10 (((0 + 1) + 2) + 3) + 4

Slide 82

Slide 82 text

...versus “right fold” (foldr, fold_right)

Slide 83

Slide 83 text

1 2 3 4 0

Slide 84

Slide 84 text

10 1 + (2 + (3 + (4 + 0)))

Slide 85

Slide 85 text

The initial argument is optional...

Slide 86

Slide 86 text

[1, 2, 3, 4]. inject { |x,y| x+y }

Slide 87

Slide 87 text

[1, 2, 3, 4]. inject { |x,y| x+y } [2, 3, 4]. inject(1) { |x,y| x+y }

Slide 88

Slide 88 text

...but only if the output is the same type as the input...

Slide 89

Slide 89 text

>> ['El', 'rug']. inject(0) { |l,s| l + s.length } => 5 >> ['El', 'rug']. inject { |l,s| l + s.length } TypeError: can't convert Fixnum into String from (irb):1:in `+' from (irb):1 from (irb):1:in `inject' from (irb):1:in `each' from (irb):1:in `inject' from (irb):1

Slide 90

Slide 90 text

...and itʼs meaningful to get nil when the collection is empty

Slide 91

Slide 91 text

>> [].inject { |x,y| x+y } => nil >> [].inject(0) { |x,y| x+y } => 0 >> [].inject(1) { |x,y| x*y } => 1

Slide 92

Slide 92 text

COMPOSE!

Slide 93

Slide 93 text

[1, 2, 3, 4]. map { |x| x * 3 }. inject(0) { |x| x+y }

Slide 94

Slide 94 text

1 2 3 4 2 1 3 4 6 3 9 12 ×3 ×3 ×3 ×3 0 0 3 9 18 30 + + + +

Slide 95

Slide 95 text

3 6 9 12 1 2 3 4 2 1 3 4 6 3 9 12 ×3 ×3 ×3 ×3 3 9 18 0 0 3 9 18 30 + + + +

Slide 96

Slide 96 text

I am so excited. What now?

Slide 97

Slide 97 text

Review your Ruby code. Func it up.

Slide 98

Slide 98 text

result = '' for name in names unless result.empty? result << ', ' end result << name end result

Slide 99

Slide 99 text

result = '' for name in names unless result.empty? result << ', ' end result << name end result names.join(', ') !

Slide 100

Slide 100 text

def count_mines_near(x, y) count = 0 for i in x-1..x+1 for j in y-1..y+1 count += 1 if mine_at?(i, j) end end count end

Slide 101

Slide 101 text

def count_mines_near(x, y) count = 0 for i in x-1..x+1 for j in y-1..y+1 count += 1 if mine_at?(i, j) end end count end def count_mines_near(x, y) ((x-1..x+1).entries * 3).sort. zip((y-1..y+1).entries * 3). select { |x, y| mine_at?(x, y) }. length end !

Slide 102

Slide 102 text

def count_mines_near(x, y) ((x-1..x+1).entries * 3).sort. zip((y-1..y+1).entries * 3). select { |x, y| mine_at?(x, y) }. length end [1, 2, 3, 1, 2, 3, 1, 2, 3] [1, 1, 1, 2, 2, 2, 3, 3, 3] .sort = # = (2, 8)

Slide 103

Slide 103 text

def count_mines_near(x, y) ((x-1..x+1).entries * 3).sort. zip((y-1..y+1).entries * 3). select { |x, y| mine_at?(x, y) }. length end [1, 2, 3, 1, 2, 3, 1, 2, 3] [1, 1, 1, 2, 2, 2, 3, 3, 3] [7, 8, 9, 7, 8, 9, 7, 8, 9] [[1, 7], [1, 8], [1, 9], [2, 7], [2, 8], [2, 9], [3, 7], [3, 8], [3, 9]] .sort = .zip( ) = # = (2, 8)

Slide 104

Slide 104 text

def count_mines_near(x, y) ((x-1..x+1).entries * 3).sort. zip((y-1..y+1).entries * 3). select { |x, y| mine_at?(x, y) }. length end [1, 2, 3, 1, 2, 3, 1, 2, 3] [1, 1, 1, 2, 2, 2, 3, 3, 3] [7, 8, 9, 7, 8, 9, 7, 8, 9] [[1, 7], [1, 8], [1, 9], [2, 7], [2, 8], [2, 9], [3, 7], [3, 8], [3, 9]] .sort = .zip( ) = .select {…} = [[1, 8], [3, 7]] .length = 2 # = (2, 8)

Slide 105

Slide 105 text

def count_mines_near(x, y) ((x-500..x+500).entries * 1001).sort. zip((y-500..y+500).entries * 1001). select { |x, y| mine_at?(x, y) }. length end [ [1, 7], [1, 8], …, [1, 1007], [2, 7], [2, 8], …, [2, 1007], …, …, …, [1000, 7], [1000, 8], …, [1000, 1007], [1001, 7], [1000, 8], …, [1001, 1007]]

Slide 106

Slide 106 text

[ [1, 7], [1, 8], …, [1, 1007], [2, 7], [2, 8], …, [2, 1007], …, …, …, [1000, 7], [1000, 8], …, [1000, 1007], [1001, 7], [1000, 8], …, [1001, 1007]] 173 96 121 78 237 705

Slide 107

Slide 107 text

def numbers_near(n, radius) ((n - radius)..(n + radius)).entries end def squares_near(x, y, radius) diameter = (radius * 2) + 1 (numbers_near(x, radius) * diameter). sort. zip(numbers_near(y, radius) * diameter) end def count_mines_near(x, y, radius = 1) squares_near(x, y, radius). select { |x, y| mine_at?(x, y) }. length end

Slide 108

Slide 108 text

Learn a functional language •OCaml •Scheme (Google “SICP”) •Clojure •Scala •Haskell? Erlang?

Slide 109

Slide 109 text

Thanks! @tomstuart / tom@experthuman.com

Slide 110

Slide 110 text

http://www.flickr.com/photos/somemixedstuff/2403249501/ http://en.wikipedia.org/wiki/File:Modified-pc-case.png