“Data dominates. If you’ve chose the right data
structures & organise things well, the algorithms
will almost be self-evident. Data structures, not
algorithms, are central to programming”
- Rob Pike
Slide 22
Slide 22 text
Step-2
Define The Behaviour
(Algorithm)
Slide 23
Slide 23 text
“Make each program do one thing well. To do a
new job, build afresh rather than complicate the
old programs by adding new “features”.
Expect the output of every program to become
the input of another, as yet unknown, program”
- Unix Philosophy
Slide 24
Slide 24 text
Step-3
Define The System
(Composition)
Slide 25
Slide 25 text
“Design is to take things apart in a such a way
that they can be put back together”
- Rich Hickey
Slide 26
Slide 26 text
Separation of Concerns
Composition
Slide 27
Slide 27 text
Dynamic
HTML Generation
Slide 28
Slide 28 text
Step-1
Model The Domain
(Data)
Slide 29
Slide 29 text
type Attribute = {
Name : string
Value : string
}
Slide 30
Slide 30 text
type Element = {
Name : string
Attributes : Attribute list
}
Slide 31
Slide 31 text
This is a paragraph
type Element = {
Name : string
Attributes : Attribute list
Content : Content
} and Content =
PlainText of string
Slide 32
Slide 32 text
type Element = {
Name : string
Attributes : Attribute list
Content : Content option
} and Content =
PlainText of string
Slide 33
Slide 33 text
Coffee
Tea
type Element = {
Name : string
Attributes : Attribute list
Content : Content option
} and Content =
| PlainText of string
| ChildElements of Element list
Slide 34
Slide 34 text
Algebraic Data Types
type Element = {
Name : string
Attributes : Attribute list
Content : Content option
} and Content =
| PlainText of string
| ChildElements of Element list
Sum (or Choice) Type
Product Type
Slide 35
Slide 35 text
Step-2
Define The Behaviour
Slide 36
Slide 36 text
Element !-> string
let beveragesList =
let content =
childElements [
element "li" [] (plainText "Tea")
element "li" [] (plainText "Coffee")]
element "ul" [("class", "ulist")] content
What we have
What we want
Content !-> string
Option
!-> Option
Slide 62
Slide 62 text
map
Slide 63
Slide 63 text
map
Slide 64
Slide 64 text
// Option
let contentOpt =
element.Content
Option.map contentToHtmlStr
Slide 65
Slide 65 text
let content =
defaultArg contentOpt ""
Option !-> string
core library function
Slide 66
Slide 66 text
What we have
What we want
Element !-> string
Option !-> string
List !-> string
Slide 67
Slide 67 text
let elementToHtmlStr element =
let attrs =
attrsToHtmlStr element.Attributes
let contentOpt =
element.Content
Option.map contentToHtmlStr
let content =
defaultArg contentOpt “"
let tag = element.Name
sprintf "<%s %s> %s %s>"
tag attrs content tag
Slide 68
Slide 68 text
let beveragesList =
let content =
childElements [
element "li" [] (plainText "Tea")
element "li" [] (plainText "Coffee")]
element "ul" [("class", "ulist")] content
Slide 69
Slide 69 text
Error Handling
Slide 70
Slide 70 text
Result (Either) Type
int !-> Result
Slide 71
Slide 71 text
// int Result
let getElementById id =
if id = 1 then
Ok beveragesList
else
Error "unable to fetch"
Slide 72
Slide 72 text
Transform Element to Html String
after fetching
Slide 73
Slide 73 text
What we have
What we want
Result
!-> Result
Element !-> string
Slide 74
Slide 74 text
map
Slide 75
Slide 75 text
map
Slide 76
Slide 76 text
// int Result
let getHtmlStrById id =
getElementById id
Result.map elementToHtmlStr
Prompt
let namePrompt : Prompt =
stringPrompt "What's Your name?”
> namePrompt();;
What’s your name?
Tamizhvendan
val it : Result = Ok “Tamizhvendan”
Slide 112
Slide 112 text
Prompt
let agePrompt : Prompt =
intPrompt "What's Your age?”
> agePrompt();;
What’s your age?
29
val it : Result = Ok 29
> agePrompt();;
What’s your age?
foobar
val it : Result = Error ParserError
Slide 113
Slide 113 text
let greeting name age =
sprintf "Hey %s, you are %d years old!” name age
let greetingWizard =
lift2 greeting namePrompt agePrompt
> greetingWizard();;
What’s your name?
Tamizhvendan
What’s your age?
29
val it : Result = Ok “Hey Tamizhvendan, you are 29 years old!”
lift2
Slide 114
Slide 114 text
let addAgeBy10Years age = age + 10
let prankWizard =
agePrompt
|> map addAgeBy10Years
|> lift2 greeting namePrompt
> prankWizard();;
What’s your name?
Tamizhvendan
What’s your age?
29
val it : Result = Ok “Hey Tamizhvendan, you are 39 years old!”
map
By The Way!
M
AP
F Functor
Applicative
Monad
* There are some laws!
Slide 118
Slide 118 text
FP Demystified
λ Algebraic Data Types
λ Separate Pure & Impure Functions
λ Function Signature Matters!
λ map, liftN, bind, etc.,
λ Have Fun!!
https://www.demystifyfp.com