This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Modern software becomes larger and more complexWe’d like to reduce the time and cost of software developmentWe’d like to increase the quality of the developed software
FP to the Rescue
Promotes composition and modularityPromotes simpler codeConcise, promotes smaller codeConcurrency is not an issueDramatically reduces number of bugs
The Goal of this Presentation
Is not to convince to abandon OOPIs to open your mind for additional paradigmLearn techniques which you can utilize in your favorite programming language
sorted :: [Integer] -> Boolsorted [] = Truesorted [_] = Truesorted (x:y:zs) = x < y && sorted (y:zs)
So, What Is It All About?Functional Programming Other programming styles
Classes, procedures Building Blocks
N/A A must State changes
Function calls, Loops, conditionals, and function (method) calls
Main control flow
Don’t Panic!!!
FP Principles
ImmutabilityPure functionsModularity and compositionDeclarative
Immutability
Thread safeData flow is explicitCacheable 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.”
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
A function has no side effect We gain clarity We gain “laziness”:
int foo(){
bar();return 0;
}
Pure Functions
Function calls are independentCan be parallelized
result = func1(x,y) + func2(x,z)
Pure Functions
Pure Functions
Encapsulation is in the function levelEasier to debugEasier to test in isolation
Modularity and Composability
A function’s output is only its return valueWe gain:
Modularity with light couplingComposability
Declarative
The focus is on what the computer should do rather than how it should do itThink of a spreadsheet:
We don’t specify the order of calculationsWe don’t deal with variablesWe deal with expressions and not with stepsWe tell it what we want not how to achieve it
Think of SQL…FP is the same but for general purpose
Imperative
List<int> results = new List<int>();foreach(var num in collection){ if (num % 2 != 0) results.Add(num);}
Declarative vs Imperative
Declarativevar results = collection.Where( num => num % 2 != 0);