Functional Programming

download Functional Programming

of 34

  • date post

  • Category


  • view

  • download


Embed Size (px)


Overview of functional programming and functional programming in C#.

Transcript of Functional Programming

  • 1.
    Functional Programming

2. Who am I?
Ryan Riley
Catapult Systems Houston
Twitter: @panesofglass
3. Agenda
Functional Programming Concepts
Funcy C#
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.
10. Immutability
NO side effects!
NO state!
Functions take an input and return a result
Image from One Year Bible Blog
11. Implications
Persistent data structures
12. Paradigm-shift
13. Advantages
Very easy to reason about
Concurrency friendly
Easy to compose:doubleSq x = square(double(x))doubleSq = double . square
Foundation of Domain Specific Languages
14. Disadvantages
Too much recursion can overflow the stack (without tail-call optimization, or TCO)
High memory consumption from creating so many objects
15. However,
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!
18. Also,
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
25. Demo
Currying and Partial application
26. Make it concurrent
Task Parallel Library (PFx)
Reactive Extensions (Rx)
PLinq => Parallel LINQ
27. Demo
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
31. Libraries
Functional C#
Reactive Extensions (Rx)
32. Videos
Functional Programming Fundamentals(Channel 9 Lecture Series)
33. Languages
More F# =>
More Clojure and IronScheme =>
34. Questions?