in€¦ · thinking functionally in ruby. @tomstuart. functional programming is a pretty neat idea....

110
THINKING FUNCTIONALLY IN RUBY. @tomstuart

Upload: others

Post on 07-Sep-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

THINKINGFUNCTIONALLY

IN

RUBY.@tomstuart

Page 2: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Functional programming is a pretty neat idea.

1:

Page 3: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Enumerable contains some useful methods.

2:

Page 4: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

I hopethese things are connected.

Page 5: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

(Functional programming is a pretty neat idea)

1.

Page 6: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

What is functional

programming?

Page 7: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

A programming style.

Page 8: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Two broad families of languages:

Page 9: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1. Lisp-like

• Scheme• Common Lisp• Dylan• Clojure

• Dynamically typed• Homoiconic (code is data)• Lists are fundamental

Page 10: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

2. ML-like

• Standard ML• Haskell• OCaml• Scala (sort of)

• Statically typed (+ reconstruction)• Code is not data• ADTs and pattern matching

Page 11: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Functionsas values

Page 12: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

What isa function?

Page 13: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected
Page 14: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected
Page 15: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected
Page 16: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

172

36

45 4

26

8

371

Page 17: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Functionsas values

Page 18: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

“First-class functions”

Implies: higher-order functions

“closures”, “lambdas”,“anonymous functions”

Page 19: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

No side effects

Page 20: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Values are immutable

All functions do is return their result

No stateNo I/O

Page 21: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

(In reality,different languages

accomplish thisto varying degrees.)

Page 22: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Implies: recursion

Implies: persistent data structures

Page 23: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

So what?

Page 24: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Unfortunately...

Page 25: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Declarative programming

is counterintuitive whenyouʼre accustomed to

imperative programming

WHAT!

HOW!

Page 26: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Copying is expensive

Page 27: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

But!

Page 28: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 29: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Highly expressive

Deeply satisfying

Strongly compositional

Page 30: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 31: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

OK seriously: so what?

Page 32: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Ruby can do some of this.Not really a functional language, but we

can pretend and get some of the benefits.

Page 33: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Use function values

lambda { |x| x + 1 }

blocks, procs

Page 34: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Consider treating your values

as immutableState is rarely worth it.

It will kill you in the end.

Page 35: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

http://clojure.org/state

Page 36: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Use thefunctional-flavoured

parts of thestandard library

Page 37: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Be declarative.

Think functionally.

What, not how.

Page 38: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

2.(Enumerable contains some useful methods)

Page 39: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Enumerable#zip

Page 40: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 41: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

21 3 4

65 7 8

Page 42: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

21 3 465 7 8

Page 43: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

[ ]21 3 465 7 8[ ], , , ,[ ] [ ] [ ], , ,

Page 44: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 45: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 46: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

{ |x| x.odd? }

Page 47: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

11?

Page 48: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

11?!

Page 49: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

11?!

22?

Page 50: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

11?!

22?"

Page 51: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 52: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1 2 3 421 3 4? ? ? ?

Page 53: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 54: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1 31 3? ?! !

1 3

Page 55: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1 3[ ],

Page 56: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Enumerable#partition

Page 57: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 58: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1 2 3 421 3 4? ? ? ?

Page 59: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 60: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1?!

4?"

2?"

3?!

Page 61: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1?!

4?"

2?"

3?!

1 3 2 4

Page 62: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 63: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 64: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

{ |x| x * 3 }

Page 65: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

226×3

Page 66: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

226×3

Page 67: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 68: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1 2 3 421 3 463 9 12×3 ×3 ×3 ×3

Page 69: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1 2 3 421 3 463 9 12

×3 ×3 ×3 ×3

Page 70: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

63 9 12[ ], , ,

Page 71: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 72: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

{ |x, y| x + y }

Page 73: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

33 558+

Page 74: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

33

55 8+

Page 75: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 76: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1

2

3

4

1

2

3

4

0

1

3

6

10

+

+

+

+

Page 77: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1

1 2

3 3

6 4

01

2

3

4

0

1

3

6 10

+

+

+

+

Page 78: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

module Enumerable def inject(initial) result = initial

for element in self result = yield(result, element) end

result endend

Page 79: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 80: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1 2 3 40

Page 81: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 82: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 83: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1 2 3 4 0

Page 84: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

101 + (2 + (3 + (4 + 0)))

Page 85: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

The initial argument is optional...

Page 86: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 87: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 88: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 89: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

>> ['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

Page 90: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 91: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

>> [].inject { |x,y| x+y }=> nil

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

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

Page 92: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

COMPOSE!

Page 93: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 94: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

1 2 3 421 3 463 9 12×3 ×3 ×3 ×3

003

918

30

++

++

Page 95: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

36

912

1 2 3 421 3 4

63

912

×3 ×3 ×3 ×3

39

18

00

39

18 30

++

++

Page 96: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

I am so excited.

What now?

Page 97: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

Review your Ruby code.

Func it up.

Page 98: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

Page 99: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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

names.join(', ')

!

Page 100: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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 countend

Page 101: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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 countend

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) }. lengthend

!

Page 102: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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) }. lengthend

[1, 2, 3, 1, 2, 3, 1, 2, 3][1, 1, 1, 2, 2, 2, 3, 3, 3] .sort =

# = (2, 8)

Page 103: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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) }. lengthend

[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)

Page 104: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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) }. lengthend

[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)

Page 105: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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) }. lengthend

[ [1, 7], [1, 8], …, [1, 1007], [2, 7], [2, 8], …, [2, 1007], …, …, …, [1000, 7], [1000, 8], …, [1000, 1007], [1001, 7], [1000, 8], …, [1001, 1007]]

Page 106: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

[ [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

Page 107: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

def numbers_near(n, radius) ((n - radius)..(n + radius)).entriesend

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) }. lengthend

Page 108: IN€¦ · THINKING FUNCTIONALLY IN RUBY. @tomstuart. Functional programming is a pretty neat idea. 1: Enumerable contains some useful methods. 2: I hope these things are connected

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