functional programming in ruby
TRANSCRIPT
Functional programming in Ruby
Alex Teutaka
@jaturken
Functional Programming
A programming paradigm that treats computation as the evaluation of functions
and avoids state and mutable data.
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.
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.
Referential transparency
The same language expression can result in different values at different times
depending on the state of the executing program.
Pure Functions
● Always evaluates the same result value given the same argument values
● Evaluation of the result does not cause any side effect
Pure
● sin● to_s● Enumerable#select● Enumerable#collect● Array#uniq● Hash#merge
Non-pure
Side-effects:● print● require● Enumerable#select!● Enumerable#collect!● Array#uniq!● Hash#merge!● Hash#delete
Non-determinated:● Date.today● rand● Externall API
calls
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
Functional Programming
X = X + 1
Functional Programming
X = X + 1
Iterations without iterator?
Recursion
Example
Tail Call
A special case of recursion when recursive call of function itself is its last operation.
Example
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
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.
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
Higher-order Functions
Functions that can either take other functions as arguments or return them as
results
Closure
A function or reference to a function together with a referencing environment
Example
Currying
The technique of transforming a function that takes multiple arguments to a chain of
functions each with a single argument.
Without currying
With currying
Lazy Evaluation
An evaluation strategy which delays the evaluation of an expression until its value
is needed
Example
Advantages of Functional Programming
● Reliability● Parallelizm● Easy unit testing● Easy debugging
Disadvantages of Functional Programming
● Seems to be more difficult● Typically less efficient● Garbage collector needed● Difficulties with IO methods and states
Thank you for attention!