8. The benefits of functional
programming
● succinct, concise and understandable code
● offers different programming perspective
● FP becoming more accessible
● concurrency / parallelism without tears
23. Programs as functions
● No loops but recursion
● Minimise number of variables
● no assignment operation
● only constants, parameters and values
24.
25. The dark side
too much recursion can overflow the stack
(without tail-call optimization)
high memory consumptions from creating so
many objects
26. No variables, no assignment – No
problem
● No notion of the internal state of a function
● referential transparency
● value semantics
27. Referential Transparency
An expression is said to be referentially
transparent if it can be replaced with its value
without changing the behaviour of a program
25 == 5 * 5
28. Value semantics
It means for an object that only its value counts,
not its identity
Coping an object doesn't change the behaviour
of your program
29. Pure Function
● Always evaluates to the same result
● Evaluation does not cause side effects
30.
31. What does it mean in practice?
No bugs due to nasty side effects
35. Immutability
“Object-oriented programming makes code
understandable by encapsulating moving parts.
Functional programming makes code
understandable by minimizing moving parts.”
— Michael Feathers, author of Working with Legacy Code, via
Twitter
36. ● Data once created are never changed
● Need to “update” your data? – create new one
● Safe to share data between threads
42. JavaScript
function filter(array, test) {
var passed = [];
for (var i = 0; i < array.length; i++) {
if (test(array[i]))
passed.push(array[i]);
}
return passed;
}
43. function map(array, transform) {
var mapped = [];
for (var i = 0; i < array.length; i++)
mapped.push(transform(array[i]));
return mapped;
}
47. Lazy evaluation
● Delaying evaluation of an expression
● Reduction of memory footprint as values are created
when needed not when they are declared
● Potential to construct infinite data structures
● Requires pure functions as order of operations becomes
indeterminate
48. Lazy Fibonacci
● Haskell
● Infinite list
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
● Take only required Fibonacci numbers
49. Memoization
● Storing the result of function calls
● Returning the cached result
● Trade-off between function time cost and space
cost
● Run-time not compile-time optimisation
50.
51. You can program functionally in a variety of
programming languages
56. Code comprehension
easier to understand and reason about because
you don't have to worry about what is happening
behind the scenes
57. Modularity
modular design brings with it great productivity
improvements. Small modules can be coded
quickly and easily and have a greater chance of
re-use which leads to faster development of
programs.
Additionally, the modules can be tested
independently, thus helping to reduce the time
spent unit testing and debugging
58. Testing, debugging & automation
● PF is much easier to reason about
● specific arguments always return specific results
● problems are easy to troubleshoot/duplicate because no
state or external dependencies
● automation is simplified due to lack of environmental,
state or configuration concerns