This document provides an overview of functional programming concepts including pure functions, immutable values, algebraic data types (ADTs), pattern matching, functors, monads, and error handling. Pure functions always return the same output for the same input without side effects. ADTs allow combining other types in a structured way using product and sum types. Pattern matching allows destructuring and matching values of ADTs. Functors and monads allow sequencing computations in a functional way and handling errors and side effects through types like Maybe/Option and Either.
4. Pure function
Given the same input, will always return the same output
Doesnt have any side effects
1
2
5. 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 + " !");
}
6. 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
16. 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
22. 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