functional programming in ruby

30
Functional programming in Ruby Alex Teut aka @jaturken

Upload: alex-teut

Post on 26-Jun-2015

638 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Functional Programming in Ruby

Functional programming in Ruby

Alex Teutaka

@jaturken

Page 2: Functional Programming in Ruby

Functional Programming

A programming paradigm that treats computation as the evaluation of functions

and avoids state and mutable data.

Page 3: Functional Programming in Ruby

Imperative Programming

A programming paradigm that describes computation in terms of statements that

change a program state.

A programming paradigm that treats computation as the evaluation of functions

and avoids state and mutable data.

Page 4: Functional Programming in Ruby

The difference

Imperative functions can have side effects that may change the value of program

state, so this approach looses...

A programming paradigm that treats computation as the evaluation of functions

and avoids state and mutable data.

A programming paradigm that describes computation in terms of statements that

change a program state.

Page 5: Functional Programming in Ruby

Referential transparency

The same language expression can result in different values at different times

depending on the state of the executing program.

Page 6: Functional Programming in Ruby

Pure Functions

● Always evaluates the same result value given the same argument values

● Evaluation of the result does not cause any side effect

Page 7: Functional Programming in Ruby

Pure

● sin● to_s● Enumerable#select● Enumerable#collect● Array#uniq● Hash#merge

Page 8: Functional Programming in Ruby

Non-pure

Side-effects:● print● require● Enumerable#select!● Enumerable#collect!● Array#uniq!● Hash#merge!● Hash#delete

Non-determinated:● Date.today● rand● Externall API

calls

Page 9: Functional Programming in Ruby

Pure Functions is Good

● Can be cached/memoized● Order of calculation does not matter● Any evaluation strategy can be used● Chain can be easily paralellized

Page 10: Functional Programming in Ruby

Functional Programming

X = X + 1

Page 11: Functional Programming in Ruby

Functional Programming

X = X + 1

Page 12: Functional Programming in Ruby

Iterations without iterator?

Page 13: Functional Programming in Ruby

Recursion

Page 14: Functional Programming in Ruby

Example

Page 15: Functional Programming in Ruby

Tail Call

A special case of recursion when recursive call of function itself is its last operation.

Page 16: Functional Programming in Ruby

Example

Page 17: Functional Programming in Ruby

Expand example code

tail_call_factorial:

(fact-tail 3 1)

(fact-tail 2 3)

(fact-tail 1 6)

6

non_tail_call_factorial:

(fact 3)

(* 3 (fact 2))

(* 3 (* 2 (fact 1)))

(* 3 (* 2 1))

(* 3 2)

6

Page 18: Functional Programming in Ruby

Tail Call Optimization

Tail call is equivalent to iteration. So Tail Call Optimization(TCO) is evaluating of

Tail Call as an iteration. In Ruby TCO is turned off by default.

Page 19: Functional Programming in Ruby

First Class Objects

● can be stored in values and data structures

● can be passed as a parameter to an expression

● can be returned as the result of a expression

Page 20: Functional Programming in Ruby

Higher-order Functions

Functions that can either take other functions as arguments or return them as

results

Page 21: Functional Programming in Ruby

Closure

A function or reference to a function together with a referencing environment

Page 22: Functional Programming in Ruby

Example

Page 23: Functional Programming in Ruby

Currying

The technique of transforming a function that takes multiple arguments to a chain of

functions each with a single argument.

Page 24: Functional Programming in Ruby

Without currying

Page 25: Functional Programming in Ruby

With currying

Page 26: Functional Programming in Ruby

Lazy Evaluation

An evaluation strategy which delays the evaluation of an expression until its value

is needed

Page 27: Functional Programming in Ruby

Example

Page 28: Functional Programming in Ruby

Advantages of Functional Programming

● Reliability● Parallelizm● Easy unit testing● Easy debugging

Page 29: Functional Programming in Ruby

Disadvantages of Functional Programming

● Seems to be more difficult● Typically less efficient● Garbage collector needed● Difficulties with IO methods and states

Page 30: Functional Programming in Ruby

Thank you for attention!