3. 3
Software Development Challenges
Modern software becomes larger and more
complex
We’d like to reduce the time and cost of software
development
We’d like to increase the quality of the
developed software
4. FP to the Rescue
Promotes composition and
modularity
Promotes simpler code
Concise, promotes smaller code
Concurrency is not an issue
Dramatically reduces number of
bugs
5. The Goal of this Presentation
Is not to convince to abandon OOP
Is to open your mind for additional
paradigm
Learn techniques which you can
utilize in your favorite
programming language
http://theburningmonk.com/2015/04/dont-learn-a-syntax-learn-to-change-the-way-you-think/
6. A
A
An appetizer:
sorted :: [Integer] -> Bool
sorted [] = True
sorted [_] = True
sorted (x:y:zs) = x < y && sorted (y:zs)
So, What Is It All About?
Functional ProgrammingOther programming styles
Classes, proceduresBuilding Blocks
N/AA mustState changes
Function calls,Loops, conditionals, and
function (method) calls
Main control flow
8. Immutability
Thread safe
Data flow is explicit
Cacheable objects
"How many Haskell programmers does it take to
change a lightbulb?“ "Haskell programmers don't
"change" lightbulbs, they "replace" them. And you
must also replace the whole house at the same
time.”
9. Idempotence: Get the same results no
matter how many times a function is called
We gain the ability to cache results for
function calls
Insanity: doing the same thing over and over
again and expecting different results.
Albert Einstein
Pure Functions
10. A function has no side effect
We gain clarity
We gain “laziness”:
int foo()
{
bar();
return 0;
}
Pure Functions
11. Function calls are independent
Can be parallelized
result = func1(x,y) + func2(x,z)
Pure Functions
13. Modularity and Composability
A function’s output is only its return value
We gain:
Modularity with light coupling
Composability
14. Declarative
The focus is on what the computer should do
rather than how it should do it
Think of a spreadsheet:
We don’t specify the order of calculations
We don’t deal with variables
We deal with expressions and not with steps
We tell it what we want not how to achieve it
Think of SQL…
FP is the same but for general purpose
15. Imperative
List<int> results = new List<int>();
foreach(var num in collection)
{
if (num % 2 != 0)
results.Add(num);
}
Declarative vs Imperative
Declarative
var results = collection.Where( num => num % 2 != 0);
List<int> collection = new List<int> { 1, 2, 3, 4, 5 };
http://stackoverflow.com/a/1784702/871579
16. Myths and Misconceptions
It’s difficult
It’s not difficult it’s “just” unfamiliar
We can do it also in XXX (= your favorite
language)
Maybe, but “real” FP language is “pushing” you there
It’s not practical for real-world applications
Simply wrong
FP has poor performance
Actually some of the features gives great
optimization opportunities
The quality code vs optimized code tradeoff
17. FP is here from the ‘50s
At the beginning computers were slow
Not enough power to process abstractions
Now it’s back!
The Retro of FP
19. (Some) Functional Language Features
Separate data from behavior
Functions as First-Class Values
High-Order Functions
Recursion
Pattern matching
Algebraic data types
Lazy evaluation
20. Separate Data from Behavior
No classes and objects
Data passes between functions as parameters
and return values
21. Functions as First-Class Values
You can do with functions whatever you can do
with other data-types. E.g.:
Bind identifier to a function
Store a function in a data structure
let squareIt = fun n -> n * n
let doubleIt = fun n -> 2 * n
let funList = [ squareIt; doubleIt ]
22. Functions as First-Class Values
High order functions
let compose =
fun op1 op2 ->
// Use a let expression to build the function that will
be returned.
let funToReturn = fun n ->
op1 (op2 n)
// Then just return it.
funToReturn
let squareAndDouble = compose doubleIt squareIt
// The following expression squares 3, doubles 9, returns 18, and
// then displays 18.
System.Console.WriteLine(squareAndDouble 3)
25. Algebraic Data Types
data Bool = False | True
data Point = Point Float Float deriving (Show)
data Shape =
Circle Point Float | Rectangle Point Point deriving (Show)
surface :: Shape -> Float
surface (Circle _ r) = pi * r ^ 2
surface (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 -
x1) * (abs $ y2 - y1)
26. Lazy Evaluation
Evaluation is deferred until their results are
needed
Allows using infinite listsnats = [0..]
odds = [1,3..]
ones = 1 : ones
-- What will happen when executing the following?
length odds
take 5 odds
27. Language Comparison
F#ScalaHaskell
CLR (.Net)JVMNativeRuntime
Medium1High (-)HighFP Adherence
Default (but may
be not)
Default (but may
be not)
YesImmutability
YesNot by default (but
can be)
YesLazy Evaluation
YesYesYesAlgebraic Data
Types
1) E.g. functions are not guaranteed to be pure: let tm = DateTime.Now