Functional Programming in Ruby
Transcript of Functional Programming in Ruby
![Page 1: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/1.jpg)
Functional programming in Ruby
Alex Teutaka
@jaturken
![Page 2: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/7.jpg)
Pure
● sin● to_s● Enumerable#select● Enumerable#collect● Array#uniq● Hash#merge
![Page 8: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/10.jpg)
Functional Programming
X = X + 1
![Page 11: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/11.jpg)
Functional Programming
X = X + 1
![Page 12: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/12.jpg)
Iterations without iterator?
![Page 13: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/13.jpg)
Recursion
![Page 14: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/14.jpg)
Example
![Page 15: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/15.jpg)
Tail Call
A special case of recursion when recursive call of function itself is its last operation.
![Page 16: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/16.jpg)
Example
![Page 17: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/20.jpg)
Higher-order Functions
Functions that can either take other functions as arguments or return them as
results
![Page 21: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/21.jpg)
Closure
A function or reference to a function together with a referencing environment
![Page 22: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/22.jpg)
Example
![Page 23: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/24.jpg)
Without currying
![Page 25: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/25.jpg)
With currying
![Page 26: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/26.jpg)
Lazy Evaluation
An evaluation strategy which delays the evaluation of an expression until its value
is needed
![Page 27: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/27.jpg)
Example
![Page 28: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/28.jpg)
Advantages of Functional Programming
● Reliability● Parallelizm● Easy unit testing● Easy debugging
![Page 29: Functional Programming in Ruby](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042700/558cc16ed8b42a061a8b45a5/html5/thumbnails/30.jpg)
Thank you for attention!