KEMBAR78
Functional Programming | PPTX
ÎťFunctional Programming
Who am I?Ryan RileyCatapult Systems HoustonBlog: http://wizardsofsmart.netGitHub: http://github.com/panesofglassTwitter: @panesofglass
AgendaFunctional Programming ConceptsFunc’y C#Resources
1. Functional Programming Concepts
Functional programming is just a style.
Treat functions as values.
What is a function?A function, as in math.(In particular, as in lambda calculus.)((Ask Jeff Odell.))
Follow the Arrowsadd x y has a type of int -> int -> intIn C#, this could be Func<int, int, int>(Actually, that’s Func<int,<Func<int, int>>>. We’ll see why in a moment….)
Functions are values, too..NET 1.0 => delegates.NET 2.0 => anonymous delegates + generics.NET 3.0 => lambdasHigher-order functions accept a function as a parameter.
ImmutabilityNO side effects!NO state!NO I/O!	Functions take an input and return a resultImage from One Year Bible Blog
ImplicationsRecursionPersistent data structures
Paradigm-shiftDeclarativevs.ImperativeWhat?How?
AdvantagesVery easy to reason aboutConcurrency friendlyEasy to compose:doubleSq x = square(double(x))doubleSq = double . squareFoundation of Domain Specific Languages
DisadvantagesToo much recursion can overflow the stack (without tail-call optimization, or TCO)High memory consumption from creating so many objects …
However,
Referential transparencyReplace a function with its valueEfficiency with caching (a.k.a. memoization)Easier for programmers to understand∴ Easier to reuse and test
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!
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.
2. Play that Func’yMusic C#
Func & ActionDefine your functions.Generics are important.Type-safety over many types.
Lists everywhere!Functional languages like Lisp build entirely on lists and maps (i.e. Dictionaries).Feel like you’ve seen this before?
Also by Erik Meijer!LINQ!Implementation of common functional programming functions over lists via IEnumerable.Select => mapWhere => filterAggregate => fold / reduceSelectMany => bind / fmap, or “map then flatten”
This means you are probably already using FP!
Take it furtherFunctions as dataClosures => Capture external state for laterCurrying + Partial application =>Reduce dependenciesMemoization => Run it again later
DemoCurrying and Partial application
Make it concurrentTask Parallel Library (PFx)Reactive Extensions (Rx)PLinq => Parallel LINQ
DemoConcurrency
What didn’t we cover?Many more functional operators (LINQ)Managing mutable state from the environmentLINQ to Observables, Continuations, etc.The “M” word
3. Resources
Books ‘n BlogsReal-World Functional ProgrammingPFx Team BlogRx Team BlogMatthew Podwysocki’s Weblog
LibrariesFunctional C#Reactive Extensions (Rx)
VideosFunctional Programming Fundamentals(Channel 9 Lecture Series)
LanguagesMore F# => http://c4fsharp.groups.live.comMore Clojure and IronScheme => http://hdlug.groups.live.com
Questions?

Functional Programming