Download - Functional Programming
![Page 1: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/1.jpg)
λFunctional Programming
![Page 2: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/2.jpg)
Who am I?• Ryan Riley• Catapult Systems Houston• Blog: http://wizardsofsmart.net• GitHub: http://github.com/panesofglass• Twitter: @panesofglass
![Page 3: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/3.jpg)
Agenda1. Functional Programming Concepts2. Func’y C#3. Resources
![Page 4: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/4.jpg)
1. Functional Programming Concepts
![Page 5: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/5.jpg)
Functional programmin
g is just a style.
![Page 6: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/6.jpg)
Treat functions as
values.
![Page 7: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/7.jpg)
What is a function?
A function, as in math.(In particular, as in lambda calculus.)
((Ask Jeff Odell.))
![Page 8: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/8.jpg)
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….)
![Page 9: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/9.jpg)
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.
![Page 10: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/10.jpg)
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
![Page 11: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/11.jpg)
Implications• Recursion• Persistent data structures
![Page 12: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/12.jpg)
What?
How?
Paradigm-shift
Declarativevs.
Imperative
![Page 13: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/13.jpg)
Advantages• Very easy to reason about• Concurrency friendly• Easy to compose:
doubleSq x = square(double(x))doubleSq = double . square
• Foundation of Domain Specific Languages
![Page 14: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/14.jpg)
Disadvantages
• Too much recursion can overflow the stack (without tail-call optimization, or TCO)
• High memory consumption from creating so many objects …
![Page 15: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/15.jpg)
However,
![Page 16: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/16.jpg)
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
![Page 17: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/17.jpg)
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!
![Page 18: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/18.jpg)
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.
![Page 19: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/19.jpg)
2. Play that Func’y Music
C#
![Page 20: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/20.jpg)
Func & Action
• Define your functions.• Generics are important.• Type-safety over many types.
![Page 21: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/21.jpg)
Lists everywhere!• Functional languages like Lisp build
entirely on lists and maps (i.e. Dictionaries).
• Feel like you’ve seen this before?
![Page 22: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/22.jpg)
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”
![Page 23: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/23.jpg)
This means you are probably already using
FP!
![Page 24: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/24.jpg)
Take it further
• Functions as data• Closures => Capture external state
for later• Currying + Partial application =>
Reduce dependencies• Memoization => Run it again later
![Page 25: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/25.jpg)
DemoCurrying and Partial application
![Page 26: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/26.jpg)
Make it concurrent
• Task Parallel Library (PFx)• Reactive Extensions (Rx)• PLinq => Parallel LINQ
![Page 27: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/27.jpg)
DemoConcurrency
![Page 28: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/28.jpg)
What didn’t we cover?
• Many more functional operators (LINQ)• Managing mutable state from the
environment• LINQ to Observables, Continuations, etc.• The “M” word
![Page 29: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/29.jpg)
3. Resources
![Page 30: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/30.jpg)
Books ‘n Blogs
• Real-World Functional Programming• PFx Team Blog• Rx Team Blog• Matthew Podwysocki’s Weblog
![Page 31: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/31.jpg)
Libraries• Functional C#• Reactive Extensions (Rx)
![Page 32: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/32.jpg)
Videos• Functional Programming Fundament
als
(Channel 9 Lecture Series)
![Page 33: Functional Programming](https://reader035.vdocuments.net/reader035/viewer/2022062418/554ebe31b4c9053c4b8b47ed/html5/thumbnails/33.jpg)
Languages
More F# => http://c4fsharp.groups.live.comMore Clojure and IronScheme =>
http://hdlug.groups.live.com