Functional Programming

download Functional Programming

of 34

  • date post

    10-May-2015
  • Category

    Technology

  • view

    1.704
  • download

    0

Embed Size (px)

description

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
Blog: http://wizardsofsmart.net
GitHub: http://github.com/panesofglass
Twitter: @panesofglass
3. Agenda
Functional Programming Concepts
Funcy C#
Resources
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!
NO I/O!
Functions take an input and return a result
Image from One Year Bible Blog
11. Implications
Recursion
Persistent data structures
12. Paradigm-shift
Declarative
vs.
Imperative
What?
How?
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!
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
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
Concurrency
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# => http://c4fsharp.groups.live.com
More Clojure and IronScheme => http://hdlug.groups.live.com
34. Questions?