際際滷

際際滷Share a Scribd company logo
Functional
Programming 
Kamel Ouzouigh
Highlights
 Pure Functions
 Functional Features
 ADT
 Pattern Matching
 Exception Handling
 Monads
What is Functional Programming ?
Pure functions
Immutable values
1
2
Pure function
Given the same input, will always return the same output
Doesnt have any side effects
1
2
Pure function
// pure
Math.Max(5, 8)
"Typescript".Length
"Typescript".ToUpper()
var numbers = new List<int> { 0, 8, 9, 1 };
numbers.OrderBy(x => x)
numbers.Where(x => x % 2 == 0)
// impure
var numbers = new List<int> { 0, 8, 9, 1 };
numbers.Sort();
Action<String> greeting = name =>
{
Console.WriteLine("hi" + name + " !");
}
Bene鍖ts of pure functions
Easier to reason about
1
2
3
4
5
6
7
Easier to combine
Offer Referential Transparency
Easier to parallelize
Are Memoizable
Can be lazy
Easier to test
How about I/O ?
I/O Wrapper code
Functional features
What is a Function?
Map between values (ex: string -> int)
Methods, Lambdas, Maps
1
2
Functions are values
Full Applied Function
Partially Applied Function
Currying
Currying
Example
Pattern Matching
Algebraic Data Type(ADT)
Algebra consists of:
A set of objects
The operations that can be applied to those objects to create new objects
1
2
Examples
Numeric Algebra
Relational Algebra
Algebraic Data Type(ADT)
Algebraic Data Types are combination of other types
Algebraic Data Type(ADT)
Algebraic Data Type(ADT)
Product Type
Algebraic Data Type(ADT)
Algebraic Data Type(ADT)
Sum Type
Pattern Matching
A pattern match combines an applicability test and destructuring bind
Destructuring bind extract values from an object
Applicability test determines if the pattern matches the target
1
2
Pattern Matching
Over Composition
Pattern Matching
Over Inheritance / Closed Hierarchy
Pattern Matching
Over Inheritance / Closed Hierarchy
Pattern Matching
Over Inheritance / Closed Hierarchy
Pattern Matching
Over Inheritance / Closed Hierarchy / C#8
Handling Errors
Null Exception Problem
Alternatives to Null
How to deal with Null value ?
FP Solution
SAFETY BOX
Null Check FP Solution
Null Check FP Solution
Accessing Values
Functor
Functor
Accessing Values
Functor
Monad
Monad
鍖atMap aka Bind
Monad
Monad
Option aka Maybe
Option aka Maybe
Either Type
Either Type
Either Type
Either Type
Other Monads
IO
Try
State
Writer
Reader
Seq
Eval
Future

More Related Content

Functional Programming