Functional Programming
Main styles of programming / paradigms:
Imperative Programming (IP) = computation in terms of statements that change a program state from initial to result
Object-oriented Programming (OOP) = computation based on manipulation on collections of objects. Objects have internal state and support methods that query or modify this internal state in some way.
Declarative Programming (DP) = expressing the logic of a computation without describing its control flow
Functional Programming (FP) = computation as the evaluation of mathematical functions and avoids state and mutable data apply transformation (and compositions)
In other words:
Imperative Programming
a sequence of steps/instructions that happen in order (some algorithm) to calculate needed result
Functional Programming
a set of functions that transform data into needed result
Per language:
Imperative programming
C, C++
Pascal
Basic, Sh
Python
OOP
Java
Smalltalk
C++
Python
Declarative programming
Functional programming
Haskell
Scheme
OCaml
Python
Logic programming (Prolog, Clojure core.logic)
Python is flexible allowing most programming styles
What is mostly used in Imperative Programming paradigm:
while statements
for loops
functions
assignments and changing variables
How Functional Programming is different:
loops -> recursion,
map
,filter
,reduce
, comprehensionschanging variables -> no changing variables once set, instead - immutable types:
tuple
,frozenset
functions -> return value is only dependent on this function's input
Abstract and not abstract examples:
Imperative | Functional |
---|---|
Task:
Evaluate expression ignoring some mistakes:
"1+2+22+++100++48"
We need to calculate in this manner: 1+2+22+100+48
Imperative approach
🪄 Code:
📟 Output:
Functional approach
🪄 Code:
📟 Output:
Don't freak out - we'll cover all of this.
One more example that shows how different reading of some code weritten via imperative programming style or functional is.
Let's try to guess quickly what the following two functions do.
Imperative:
Functional:
Yes, both of them are case-insensitively counting 'a'
, 'b'
and 'c'
characters in a string:
🪄 Code:
📟 Output:
Functional is a bit easier to understand...
Map
map(function, iterable1, iterable2, ..., iterableN)
Return an iterator that computes the function using arguments from each of the iterables. Stops when the shortest iterable is exhausted.
On Python 2 - map returns list.
For Python 3 sometimes it's better to use list comprehensions
🪄 Code:
📟 Output:
🪄 Code:
📟 Output:
🪄 Code:
📟 Output:
Filter
filter(function, iterable)
Construct an iterator from those elements of iterable for which function returns
True
🪄 Code:
📟 Output:
Zip
zip(*iterables)
Make an iterator that aggregates elements from each of the iterables.
Simply speaking zip()
creates pairs from elements of provided arguments
🪄 Code:
📟 Output:
## Other perls of FP
Avoid state –
Immutable data –
First-class functions –
Higher-order functions –
Pure functions –
Recursion, tail recursion –
Iterators, sequences, lazy evaluation, pattern matching, monads...
Last updated