Functional Programming Fundamentals
Functional Programming Fundamentals
Fundamentals
Elements of Functional Programming
“
It’s really clear that the imperative style of
programming has run its course. ... We’re sort
of done with that. … However, in the
declarative realm we can speculate a 10x
improvement in productivity in certain
domains. -Anders Hejlsberg
C# Architect
}
}
}
}
More Noise }
}
March 30, 2010
Than Signal!
Shahriar Hyder Kaz Software Ltd.
35
Refactoring “hole in the middle”
Header() { ■ ■ ■ }
Footer() { ■ ■ ■ }
Red() { ■ ■ ■ } Factor
Factor out
out the
the differences
differences and
and the
the
Blue() { ■ ■ ■ } similarities?!
similarities?!
Foo() Bar()
{ {
Header(); Header();
Red(); Blue();
Footer(); Footer();
} }
The
The “FP
“FP Way”
Way” isis to
to simply
simply pass
pass in
in an
an
implementation
implementation of of the
the “hole”
“hole” to
to be
be
filled:
filled:
FooBar( {{■■ ■■ ■});
FooBar( ■});
Problem:
Problem: You
You want
want to
to sort
sort lists
lists of
of
GasResults
GasResults by
by various
various keys.
keys.
Array.Sort<GasResult>(results, GasResult.GasPriceComparison);
Array.Sort<GasResult>(results, GasResult.GasDistanceComparison);
(extension)
(extension) IOrderedSequence<TSource>
IOrderedSequence<TSource>
IEnumerable<TSource>.OrderBy<TSource,
IEnumerable<TSource>.OrderBy<TSource, TKey>
TKey>
(Func<TSource,
(Func<TSource, Tkey>
Tkey> keySelector)
keySelector)
March 30, 2010 Shahriar Hyder 42
Kaz Software Ltd.
March 30, 2010 Shahriar Hyder 43
Kaz Software Ltd.
First-class function
• In computer science, a programming language
is said to support first-class functions if it
treats functions as first-class objects.
Specifically, this means that the language
supports constructing new functions during
the execution of a program, storing them in
data structures, passing them as arguments to
other functions, and returning them as the
values of other functions.
March 30, 2010 Shahriar Hyder 44
Kaz Software Ltd.
Closure (computer science)
• In computer science, a closure is a
first-class function with free variables that are
bound in the lexical environment. Such a
function is said to be "closed over" its free
variables. A closure is defined within the scope
of its free variables, and
the extent of those variables is at least as long
as the lifetime of the closure itself..
• A function may create a closure and return it, as in the following example:
// Return a function that approximates the derivative of f
// using an interval of dx, which should be appropriately small.
function derivative(f, dx) {
return function (x) {
return (f(x + dx) - f(x)) / dx;
};
}
• Because the closure in this case outlives the scope of the function that creates it, the variables f and dx live on after the
function derivative returns. In languages without closures, the lifetime of a local variable coincides with the execution of the
scope where that variable is declared. In languages with closures, variables must continue to exist as long as any existing
closures have references to them. This is most commonly implemented using some form of garbage collection.
//f is a function
function derivative(f) {
return function(x) {
//approximation of derivative
return (f(x + 0.00001) f(x)) / 0.00001;
}
}
.Where<GasResult>(Func<GasResult,
.Where<GasResult>(Func<GasResult, bool>
bool> predicate)
predicate)
.Select<GasResult,
.Select<GasResult, double>(Func<GasResult,
double>(Func<GasResult, double>
double> mapping)
mapping)
.Aggregate<double,
.Aggregate<double, double>(double
double>(double seed,
seed, Func<double,
Func<double, double,
double, double>
double> func)
func)
.Where<GasResult>(Func<GasResult,
.Where<GasResult>(Func<GasResult, bool>
bool> predicate)
predicate)
.Select<GasResult,
.Select<GasResult, double>(Func<GasResult,
double>(Func<GasResult, double>
double> mapping)
mapping)
.Aggregate<double,
.Aggregate<double, double>(double
double>(double seed,
seed, Func<double,
Func<double, double,
double, double>
double> func)
func)
.Where<GasResult>(Func<GasResult,
.Where<GasResult>(Func<GasResult, bool>
bool> predicate)
predicate)
.Select<GasResult,
.Select<GasResult, double>(Func<GasResult,
double>(Func<GasResult, double>
double> mapping)
mapping)
.Aggregate<double,
.Aggregate<double, double>(double
double>(double seed,
seed, Func<double,
Func<double, double,
double, double>
double> func)
func)
• System.Concurrency Library
http://msdn.microsoft.com/msdnmag/issues/07/10/Futures/default.aspx
• Google MapReduce
http://labs.google.com/papers/mapreduce.html
• Thank You!
Questions?