Transcript function

The secret of
Functional Programming
revealed!
What we have heard..
• In Functional Programming you
only use functions!
o So no classes or objects..?
What we have heard..
• In Functional Programming you
have no variables!
Functional Programming
seems like a couple of steps
backwards..
"Functional Languages are at a higher level
of abstraction than object-oriented
languages. Though they are more difficult to
understand, you can express bigger ideas
with less code."
Bruce A. Tate
Seven Languages in Seven Weeks
Why FP is incredible!
• Reduces bugs
o easier to test
o easier to trust
• More compact code
o no variables
• More elegant
o closer to mathematics
o proofs of correctness
And the multi-core hype...
• Functional Programming promises to make concurrent and
parallell programming easier
Language differ in their support of
Functional Programming
Key Concept
Referential transparency:
A function always returns the same
result, as long as the same arguments
are passed into it.
No side effects!
The purpose of a function is to do nothing other than
return a result.
•
•
•
•
No variables must change (be mutated)
No visible effects (dialog box, "bing", etc.)
No access of external data (files, keyboard, etc.)
No reference to variables defined elsewhere
Functional programmers say side
effects makes your code dirty!
The technical term:
Imperative code
Imperative code is the opposite of
functional code
(What about OOP?)
Euler problem #1:
If we list all the natural
numbers below 10 that are
multiples of 3 or 5, we get 3, 5,
6 and 9. The sum of these
multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Imperative solution..
static int Solution(int limit)
{
int sum = 0;
for (int i = 1; i < limit; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
return sum;
}
Recursive solution..
static int Solution(int limit)
{
if (limit == 0) return 0;
int c = limit - 1;
if (c % 3 == 0 || c % 5 == 0)
{
return c + Solution(c);
}
else
{
return Solution(c);
}
}
Using Linq..
static int Solution(int limit)
{
return Enumerable.Range(1, limit - 1)
.Where(i =>
i % 3 == 0 || i % 5 == 0)
.Sum();
}
Learn to love starting functions with return!
FP is insane!
"All you can do without side effects is push a
button and watch the box get hot for a while."
Simon Peyton Jones
So what's the secret?
There must be some kind
of magic that makes you
able to implement "normal"
programs..
Or..?
The Philosophy of FP
You should break your program into two parts:
• The biggest part:
o Completely functional, free of side effects
o This is the clean part!
• The smaller part:
o Has all the side effects
o Interacts with the user / rest of the world
o This is the dirty part!
A program like this is written in a functional style.
(Compare to MVC)
Getting started with FP
• Start using System.Linq for everything:
Where
(a.k.a. filter / select)
Select
(a.k.a. map / project)
Aggregate
(a.k.a. fold / reduce)
SelectMany
(a.k.a. mapcat)
• Practice the clean/dirty separation rules
o
o
o
o
• If time, learn a FP language, then take what you've learned
back