Functional Programming

2,094 views

Published on

Overview of functional programming and functional programming in C#.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,094
On SlideShare
0
From Embeds
0
Number of Embeds
157
Actions
Shares
0
Downloads
65
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Functional Programming

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

×