Alex Functional Programming
Alex Functional Programming
Mindset
Conclusions
Intro
Why I care about functional programming
The evolution of the software industry is just recycling old ideas, and
functional programming is old.
More restrictions on code (eg. immutability) lead to better code and better
designs.
Disclaimer
Source: https://wiki.haskell.org/Monad
A shorter explanation
Wikipedia
Only one reaction
Whyyyyyyyyyyyyy?
A practical view on functional
programming
What I thought it was:
Start from input data and apply a set of transformations to get it to the
desired output.
Structured programming:
incrementAllElements(list& aList){
for(int i = 0; i < aList.size(); ++i){
aList[i] ++;
}
}
Functional programming:
Structured programming:
string result;
for(int i = 0; i < 5; ++i){
result += ’t’;
}
Example 2 (cont’d)
Functional programming:
OOP
Let’s create classes for: Pacman, Board, Wall, Dot, Movement etc.
Example 3 (cont’d)
Functional programming
……>……
Data out: a line with pacman moving one square to the right, and a missing
dot
…… >…..
Transformations:
A function that returns the same output whenever receiving the same input
auto a = Array<int>::empty();
a = a->push(1);
a = a->push(2);
a = a->push(3);
Lambdas / Anonymous functions
// Lambda variable
auto increment = [](auto value){return value + 1;};
assert(increment(2) == 3);
Lambda variable with specific captured context
More details:
http://en.cppreference.com/w/cpp/language/lambda
Chaining functions
// Without curry
list = add(list, 5);
list = add(list, 1000);
assert(incrementFunctionResult(function1) == 2);
Short list of higher level functions
Find them in or
• find_if
• transform
• reduce / accumulate
• count_if
• all_of / any_of / none_of
• …
CHECK(IF(TRUE)(”foo”)(”bar”) == ”foo”);
Source: https://github.com/alexboly/ChurchEncodingCpp
Conclusions
Transformations:
• validation
• sanitization
• canonicalization
• business rules
• save to database (mutable!)
• or read from database (mutable!)
• convert to view
• pass on to html rendering
– via JB Rainsberger
Remember first example?
add(element){....}
removeLast(){....}
}
// Functional version
add(list, element)
removeLast(list)
// Equivalence
// Or curry (bind)
def oopAdd = bind(add, initialListStorage)
oopAdd(5)
Conclusions
My thoughts
Good:
Hidden loops:
https://www.slideshare.net/alexboly/hidden-loops
https://mozaicworks.com/training/c-plus-plus/
Thank you!
at Mozaic Works
https://mozaicworks.com
Q&A
Q&A