Introduction to Functional Programming to OO programmers.
It compares functional programming with other paradigms, and explains the idea of high order functions with "filter", "map" and "reduce". Such concepts are available in many OO programming languages like Java, Ruby and Python.
Code examples in Javascript and Clojure:
https://github.com/rafaelportela/intro-functional-programming
Presented in July 2015, Johannesburg - South Africa
6. Imperative paradigm
describes what a program should do in details
steps changing the state of a program (using
variables)
can be procedural or object oriented
7. Procedural
world
programs are composed by
procedures, subroutines or
subprograms
there's no significant difference
between the main program
(main function) and all other
sub routines
program execution changes
the state of previous allocated
variables -> output of a
program is the final state of its
variables
Object
Oriented
world
Functional
world
programs are composed by
objects, which have state and
behaviour
a method call (action,
behavior) changes the state of
an object, we say an object
"sends a message" to another
object
program output is the final
state of its objects after some
interaction
programs are modeled using
only functions (composed by
other functions) and
input/output data (arrays, maps
etc, of string, numbers etc)
no side effects: functions only
know about what is inside their
boxes (pure functions, or
input-output functions)
data is immutable, no
variables, output of a program
is the output data of a function
All the worlds can be mixed - is it good or bad?
15. Map, Reduce and Filter
● Functions to be applied to collections
● Available in most of the languages (both OO
and functional ones)
● They are examples of a generic “big box”
function using a problem specific “small box”
function
17. is it red?
specific small box function: gets a single cup, and returns true if it’s red, false otherwise
Predicate function -> returns true or false
based on given argument (input)
Filter
18. Filter
is it red?
big box function: uses your specific predicate function to filter a collection
20. Map
specific small box function: gets a single piece of clothing, and creates a similar blue one
Transformation/mapping function ->
transforms/maps one single element into another
21. Map
big box function: uses your specific mapping function to map an entire collection
23. Reduce
big box function: uses your specific function on each item and accumulates the results in a single place
Reduce -> also known as fold, accumulate,
aggregate, compress or inject
24. Curiosity: More broadly, MapReduce is also
considered a “programming model”
OtherBuzzyWordHere
SoYourFriendsAreGonna
ThinkYourAreVerySmart.jpg