Functional Programming
-
Upload
ryan-riley -
Category
Technology
-
view
1.713 -
download
0
description
Transcript of Functional Programming
λFunctional Programming
Who am I?• Ryan Riley• Catapult Systems Houston• Blog: http://wizardsofsmart.net• GitHub: http://github.com/panesofglass• Twitter: @panesofglass
Agenda1. Functional Programming Concepts2. Func’y C#3. Resources
1. Functional Programming Concepts
Functional programmin
g is just a style.
Treat functions as
values.
What is a function?
A function, as in math.(In particular, as in lambda calculus.)
((Ask Jeff Odell.))
Follow the Arrows
• add x y has a type of int -> int -> int• In C#, this could be Func<int, int,
int>
• (Actually, that’s Func<int,<Func<int, int>>>. We’ll see why in a moment….)
Functions are values, too.
• .NET 1.0 => delegates• .NET 2.0 => anonymous delegates
+ generics• .NET 3.0 => lambdas• Higher-order functions accept a
function as a parameter.
ImmutabilityFunctions take an input and return a result
NO side effects!
NO state!
NO I/O!
Imag
e f
rom
One Y
ear
Bib
le B
log
Implications• Recursion• Persistent data structures
What?
How?
Paradigm-shift
Declarativevs.
Imperative
Advantages• Very easy to reason about• Concurrency friendly• Easy to compose:
doubleSq x = square(double(x))doubleSq = double . square
• Foundation of Domain Specific Languages
Disadvantages
• Too much recursion can overflow the stack (without tail-call optimization, or TCO)
• High memory consumption from creating so many objects …
However,
Referential transparency• Replace a function with its value• Efficiency with caching (a.k.a.
memoization)
• Easier for programmers to understand• ∴ Easier to reuse and test
So what?• Moore’s Law is failing.• Multiple CPUs / cores is the future.• Controlling mutable state in
concurrency is very hard …unless you have no mutable state!
Also,“Mock objects and dependency injection are just for people who don’t know math.”
*~ Erik Meijer, Microsoft
* See the Silverlight Toolkit for test examples.
2. Play that Func’y Music
C#
Func & Action
• Define your functions.• Generics are important.• Type-safety over many types.
Lists everywhere!• Functional languages like Lisp build
entirely on lists and maps (i.e. Dictionaries).
• Feel like you’ve seen this before?
Als
o
by
Eri
k M
eijer
!
LINQ!• Implementation of common functional
programming functions over lists via IEnumerable.
• Select => map• Where => filter• Aggregate => fold / reduce• SelectMany => bind / fmap, or “map then
flatten”
This means you are probably already using
FP!
Take it further
• Functions as data• Closures => Capture external state
for later• Currying + Partial application =>
Reduce dependencies• Memoization => Run it again later
DemoCurrying and Partial application
Make it concurrent
• Task Parallel Library (PFx)• Reactive Extensions (Rx)• PLinq => Parallel LINQ
DemoConcurrency
What didn’t we cover?
• Many more functional operators (LINQ)• Managing mutable state from the
environment• LINQ to Observables, Continuations, etc.• The “M” word
3. Resources
Books ‘n Blogs
• Real-World Functional Programming• PFx Team Blog• Rx Team Blog• Matthew Podwysocki’s Weblog
Libraries• Functional C#• Reactive Extensions (Rx)
Videos• Functional Programming Fundament
als
(Channel 9 Lecture Series)
Languages
More F# => http://c4fsharp.groups.live.comMore Clojure and IronScheme =>
http://hdlug.groups.live.com