Functional programming in Ruby                           Alex Teut                                 aka                    ...
Functional Programming  A programming paradigm that treatscomputation as the evaluation of functions   and avoids state an...
Imperative Programming  A programming paradigm that treatscomputation as the evaluation of functions   and avoids state an...
The difference  A programming paradigm that treatscomputation as the evaluation of functions   and avoids state and mutabl...
Referential transparencyThe same language expression can result   in different values at different times depending on the ...
Pure Functions●   Always evaluates the same result value    given the same argument values●   Evaluation of the result doe...
Pure●   sin●   to_s●   Enumerable#select●   Enumerable#collect●   Array#uniq●   Hash#merge
Non-pureNon-determinated:       Side-effects:●   Date.today      ●   print●   rand            ●   require●   Externall API...
Pure Functions is Good●   Can be cached/memoized●   Order of calculation does not matter●   Any evaluation strategy can be...
Functional ProgrammingX=X+1
Functional ProgrammingX=X+1
Iterations without iterator?
Recursion
Example
Tail CallA special case of recursion when recursive call of function itself is its last operation.
Example
Expand example codetail_call_factorial:     non_tail_call_factorial:(fact-tail 3 1)          (fact 3)(fact-tail 2 3)      ...
Tail Call OptimizationTail call is equivalent to iteration. So TailCall Optimization(TCO) is evaluating of          Tail C...
First Class Objects●   can be stored in values and data    structures●   can be passed as a parameter to an    expression●...
Higher-order Functions   Functions that can either take otherfunctions as arguments or return them as                 resu...
Closure  A function or reference to a functiontogether with a referencing environment
Example
Currying The technique of transforming a functionthat takes multiple arguments to a chain of  functions each with a single...
Without currying
With currying
Lazy Evaluation An evaluation strategy which delays theevaluation of an expression until its value                is needed
Example
Advantages of Functional                        Programming●   Reliability●   Parallelizm●   Easy unit testing●   Easy deb...
Disadvantages of Functional                       Programming●   Seems to be more difficult●   Typically less efficient●  ...
Thank you for attention!
Upcoming SlideShare
Loading in …5
×

Functional Programming in Ruby

753 views
702 views

Published on

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
753
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
16
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Functional Programming in Ruby

  1. 1. Functional programming in Ruby Alex Teut aka @jaturken
  2. 2. Functional Programming A programming paradigm that treatscomputation as the evaluation of functions and avoids state and mutable data.
  3. 3. Imperative Programming A programming paradigm that treatscomputation as the evaluation of functions and avoids state and mutable data.A programming paradigm that describescomputation in terms of statements that change a program state.
  4. 4. The difference A programming paradigm that treatscomputation as the evaluation of functions and avoids state and mutable data.A programming paradigm that describescomputation in terms of statements that change a program state.Imperative functions can have side effects that may change the value of program state, so this approach looses...
  5. 5. Referential transparencyThe same language expression can result in different values at different times depending on the state of the executing program.
  6. 6. Pure Functions● Always evaluates the same result value given the same argument values● Evaluation of the result does not cause any side effect
  7. 7. Pure● sin● to_s● Enumerable#select● Enumerable#collect● Array#uniq● Hash#merge
  8. 8. Non-pureNon-determinated: Side-effects:● Date.today ● print● rand ● require● Externall API ● Enumerable#select! calls ● Enumerable#collect! ● Array#uniq! ● Hash#merge! ● Hash#delete
  9. 9. 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
  10. 10. Functional ProgrammingX=X+1
  11. 11. Functional ProgrammingX=X+1
  12. 12. Iterations without iterator?
  13. 13. Recursion
  14. 14. Example
  15. 15. Tail CallA special case of recursion when recursive call of function itself is its last operation.
  16. 16. Example
  17. 17. Expand example codetail_call_factorial: non_tail_call_factorial:(fact-tail 3 1) (fact 3)(fact-tail 2 3) (* 3 (fact 2))(fact-tail 1 6) (* 3 (* 2 (fact 1)))6 (* 3 (* 2 1)) (* 3 2) 6
  18. 18. Tail Call OptimizationTail call is equivalent to iteration. So TailCall Optimization(TCO) is evaluating of Tail Call as an iteration. In Ruby TCO is turned off by default.
  19. 19. 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
  20. 20. Higher-order Functions Functions that can either take otherfunctions as arguments or return them as results
  21. 21. Closure A function or reference to a functiontogether with a referencing environment
  22. 22. Example
  23. 23. Currying The technique of transforming a functionthat takes multiple arguments to a chain of functions each with a single argument.
  24. 24. Without currying
  25. 25. With currying
  26. 26. Lazy Evaluation An evaluation strategy which delays theevaluation of an expression until its value is needed
  27. 27. Example
  28. 28. Advantages of Functional Programming● Reliability● Parallelizm● Easy unit testing● Easy debugging
  29. 29. Disadvantages of Functional Programming● Seems to be more difficult● Typically less efficient● Garbage collector needed● Difficulties with IO methods and states
  30. 30. Thank you for attention!

×