Embed Size (px)
Transcript of Functional Programming
2. Who am I?
Catapult Systems Houston
Functional Programming Concepts
4. 1. Functional Programming Concepts
5. Functional programming is just a style.
6. Treat functions as values.
7. What is a function?
A function, as in math.
(In particular, as in lambda calculus.)
((Ask Jeff Odell.))
8. Follow the Arrows
add x y has a type of int -> int -> int
In C#, this could be Func
(Actually, thats Func. Well see why in a moment.)
9. 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.
NO side effects!
Functions take an input and return a result
Image from One Year Bible Blog
Persistent data structures
Very easy to reason about
Easy to compose:doubleSq x = square(double(x))doubleSq = double . square
Foundation of Domain Specific Languages
Too much recursion can overflow the stack (without tail-call optimization, or TCO)
High memory consumption from creating so many objects
16. 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
17. So what?
Moores Law is failing.
Multiple CPUs / cores is the future.
Controlling mutable state in concurrency is very hard unless you have no mutable state!
Mock objects and dependency injection are just for people who dont know math.*
~ Erik Meijer, Microsoft
* See the Silverlight Toolkit for test examples.
19. 2. Play that FuncyMusic C#
20. Func & Action
Define your functions.
Generics are important.
Type-safety over many types.
21. Lists everywhere!
Functional languages like Lisp build entirely on lists and maps (i.e. Dictionaries).
Feel like youve seen this before?
22. Also by Erik Meijer!
Implementation of common functional programming functions over lists via IEnumerable.
Select => map
Where => filter
Aggregate => fold / reduce
SelectMany => bind / fmap, or map then flatten
23. This means you are probably already using FP!
24. Take it further
Functions as data
Closures => Capture external state for later
Currying + Partial application =>Reduce dependencies
Memoization => Run it again later
Currying and Partial application
26. Make it concurrent
Task Parallel Library (PFx)
Reactive Extensions (Rx)
PLinq => Parallel LINQ
28. What didnt we cover?
Many more functional operators (LINQ)
Managing mutable state from the environment
LINQ to Observables, Continuations, etc.
The M word
29. 3. Resources
30. Books n Blogs
Real-World Functional Programming
PFx Team Blog
Rx Team Blog
Matthew Podwysockis Weblog
Reactive Extensions (Rx)
Functional Programming Fundamentals(Channel 9 Lecture Series)
More F# => http://c4fsharp.groups.live.com
More Clojure and IronScheme => http://hdlug.groups.live.com