functional programming (let's fall back in love with programming)
TRANSCRIPT
Functional programmingLet’s fall back in with programming
Sudipta Mukherjee
What’s Functional ProgrammingIt’s telling the computer what to get done, not how to get it done
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.
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/
Who is using Functional Programming
Microsoft’s latest Functional Programming Language F# is widely accepted.
http://fsharp.org/testimonials/
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.
A 15 floor hotel gets built in 2 days!
Can we build software that fast and
flawless ?
We can.If we have the right tools.
Oh yes, and the
requirement should
be frozen.
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
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
Functional ProgrammingCan save us from writing the same boilerplate code time and again.
How? By wrapping boilerplate codes into several functions that eventually
becomes the vocabulary of the programmers.
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
Any
Where
Count
Aggregate
ToLookup
Concat
Single
LINQPadThe 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();
DemoLet’s see some example using LINQ
Now little bit of theoryJust enough so that you see the point
Programming with functionsIt’s just high school math. Reloaded.
Simple functions
f(x) = x + 1g(x) = x + 2z(x) = x == 0
Composite functions/higher order function
g.f(x) = g(f(x)) = g(x+1) = x + 1 + 2 = x + 3
You have been already doing it
Excel Formulae
=SUM(AVERAGE(A1:A10))
LINUX/Powershell Pipes
ls | sort –r
Get-Process | Sort-Object id
Function Types
Statistical
Gives a statistic of a collection.
Generators
Generates something
Filters
Filters a collection
Projectors
Projects a collection as something
T [ ]
Generators
Generates something out of nothing.
Filters
T [ ]T [ ]
T
Runs a predicate/filter for all elements of the given collection and
returns one or more matching entries.
Projectors
U [ ]T [ ]
T and U can be same
Projects a list to another of same or other type using a mapping
function
Statistical
T [ ] Boolean
T [ ] Numeric
Finds some statistical values for the given collection.
Func<> and Action<> (C# Specific)
What type is this function ?
Loop to LINQ
Thank You So Much!! Send queries to [email protected]