( What The FUNctional ??! )
Intro to Functional Programming
Stefano Paluello @palutz
(High level, abstract, agenda)
What is Functional Programming?
Functional programming is a declarative programming paradigm that emphasizes the evaluation of expressions , rather than execution of commands.
The expressions in these languages are formed by using functions to combine basic values. [Hutton ed. 2002]
Imperative
1: 2: 3: 4: 5: 6: |
|
Declarative
1:
|
|
First class ? Purity? Referen..what ???
HOF (High order functions)
Pure functions
function does not cause any (observable) side effects
(Using Javascript as pseudo-language)
1: 2: 3: 4: 5: |
|
(High Order functions)
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: |
|
Anonymous functions (or literals)
It's a function declared without any named identifier to refer to it.
1: 2: 3: |
|
(or Mathematical functions )
A (pure) function maps the domain onto the range
(pure) Function doesn't have any effect on the input value(s)
x++
(syntactic sugar for x = x + 1 )
Why can't we do that in FP ???
Immutable data => Unchanging over time or unable to change
Don't be so scared: it's already there in almost any language (a string is immutable)
1: 2: 3: 4: 5: |
|
The input (x) is not changed
We create a new value and maps the output with the input
1: 2: 3: 4: |
|
Is the function Y pure?
1: 2: 3: 4: |
|
Is the function Y pure?
1: 2: 3: 4: 5: 6: 7: |
|
Is the function pure?
1: 2: 3: |
|
Pure or not?
1: 2: |
|
Pure or not?
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: |
|
1: 2: 3: 4: 5: |
|
AKA: how to deal with only one input and one output
[Simple mode on]
You can call a function with LESS araguments and you will get back another functions with the remaining arguments
[Simple mode off]
(this is more pseudo code than js)
1: 2: 3: 4: 5: |
|
(AKA, currying on steroids)
Currying works decomposing a function in smaller chunks, one parameter at time.
Partial application use the same concept, but with more parameters (eg. 3 out of 5)
You can design library with partial function in mind (ordering the parameter)
Short explanation...
1: 2: 3: 4: 5: |
|
Monads is the most discussed, feared and (in)famous concept behind functional programming.
DISCLAIMER: you don't need to use Monads to start programming in a functional way.
DISCLAIMER2: understanding them won't hurt you and could be beneficial ;-)
Never heard about Monads, why hype?
PS: not aiming to be YATAM (Yet Another Tutorial About Monads), just a fast explanation (hopefully)
A Monad is just a monoid in the category of the endofunctors
Let's make some assumptions:
And... What do we get it here?
A Monad is just a x in the category of y
Think about Monad as a "container"
Let's not even start:
Monoid, Functors, Applicatives, Monads...
And this is the "cheat sheet"..
Source: HaskellWiki (again...)
Made with FsReveal