3. Top 5 Reasons for considering
Functional Programming
Composability: Let’s you compose solutions for complex problems easily
Lazy Evaluation: Enables you to benefit from Lazy evaluation
Immutability: Being able to write code that is side effect free
Parallelizable: You get easy parallelization options
Declarative: Lets you write very expressive code so readability increases.
And you get more done with less code.
I strongly recommend that you don’t try to remember these. Once you are
familiar with functional programming, these will seem obvious.
4. Functional Programming
Implementations
C# by LINQ
Rx.NET (Functional programming for Windows Events)
F# ( A succinct, multi-paradigm programming, functional first programming
language from Microsoft Research)
fsharp.org
Lambdas in C++ 11
http://www.cprogramming.com/c++11/c++11-lambda-closures.html
http://www.youtube.com/watch?v=5t-_wI7nFdU
LINQ Clone for C++
http://cpplinq.codeplex.com/
5. Who is using Functional Programming
Microsoft’s latest Functional Programming Language F# is widely accepted.
http://fsharp.org/testimonials/
6. Objective of this Talk
Ignite interest for Functional Programming (a.k.a FP)
We shall see how FP can help us remove boilerplate code
Provide some initial resources to get you started
Examples and Demos today will use LINQ
If people are interested, we can have a F# session later.
9. Oh yes, and the
requirement should
be frozen.
We can.
If we have the right tools.
10. A little comparison
House building
Complex by nature
Amendments are expensive
Mature Industry
Oh, I bet
Creating Software
Complex
Amendments are expensive
Mature Industry?
I doubt
We don’t have
enough generic
building blocks
11. How many times you have written code
for any of these situations
Finding if there is a match in a
collection
Finding all the elements that
matches a given condition
Finding the count of elements in a
collection that matches a set of
given conditions
Aggregating values of a collection
in a given way
Creating a histogram of all values
Creating a lookup table for all
values in a collection
Concatenating several collections
into one as in merge sort
Finding if there is only one
instance of something in a
collection
…. And so many other such trivial
things.
Boilerplate code
13. How?
By wrapping boilerplate codes into several functions that eventually
becomes the vocabulary of the programmers.
14. How many times you have written code
for any of these situations
Finding if there is a match in a
collection
Any
Finding all the elements that
matches a given condition
Where
Finding the count of elements in a
collection that matches a set of
given conditions
Count
Aggregating values of a collection
in a given way
Creating a histogram of all values
Creating a lookup table for all
values in a collection
Concatenating several collections
into one as in merge sort
Finding if there is only one
instance of something in a
collection
…. And so many other such trivial
things.
Aggregate
Boilerplate code
ToLookup
Concat
Single
15. LINQPad
The tool used in demo
The best C#/VB/F# snippet editor/compiler ever
Download from http://www.linqpad.net/
Use Dump() method to see anything. You can dump anything
DateTime.Today.Dump();
(new List<int>(){1,2,3}).Dump();
17. Now little bit of theory
Just enough so that you see the point
18. Programming with functions
It’s just high school math. Reloaded.
Simple functions
f(x) = x + 1
g(x) = x + 2
z(x) = x == 0
Composite functions/higher order function
g.f(x) = g(f(x)) = g(x+1) = x + 1 + 2 = x + 3
19. You have been already doing it
Excel Formulae
=SUM(AVERAGE(A1:A10))
LINUX/Powershell Pipes
ls | sort –r
Get-Process | Sort-Object id
20. Function Types
Statistical
Gives a statistic of a collection.
Generators
Generates something
Filters
Filters a collection
Projectors
Projects a collection as something
21. T [ ]
Generators
Generates something out of nothing.
22. Filters
Runs a predicate/filter for all elements of the given collection and
returns one or more matching entries.
T [ ] T [ ]
T
23. Projectors
Projects a list to another of same or other type using a mapping
function
T [ ] U [ ]
T and U can be same
24. Statistical
Finds some statistical values for the given collection.
T [ ] Boolean
T [ ] Numeric