functional programming (let's fall back in love with programming)

27
Functional programming Let’s fall back in with programming Sudipta Mukherjee

Upload: sudipta-mukherjee

Post on 13-Jul-2015

182 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Functional programming (Let's fall back in love with Programming)

Functional programmingLet’s fall back in with programming

Sudipta Mukherjee

Page 2: Functional programming (Let's fall back in love with Programming)

What’s Functional ProgrammingIt’s telling the computer what to get done, not how to get it done

Page 3: Functional programming (Let's fall back in love with Programming)

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.

Page 4: Functional programming (Let's fall back in love with Programming)

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/

Page 5: Functional programming (Let's fall back in love with Programming)

Who is using Functional Programming

Microsoft’s latest Functional Programming Language F# is widely accepted.

http://fsharp.org/testimonials/

Page 6: Functional programming (Let's fall back in love with Programming)

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.

Page 7: Functional programming (Let's fall back in love with Programming)

A 15 floor hotel gets built in 2 days!

Page 8: Functional programming (Let's fall back in love with Programming)

Can we build software that fast and

flawless ?

Page 9: Functional programming (Let's fall back in love with Programming)

We can.If we have the right tools.

Oh yes, and the

requirement should

be frozen.

Page 10: Functional programming (Let's fall back in love with Programming)

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

Page 11: Functional programming (Let's fall back in love with Programming)

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

Page 12: Functional programming (Let's fall back in love with Programming)

Functional ProgrammingCan save us from writing the same boilerplate code time and again.

Page 13: Functional programming (Let's fall back in love with Programming)

How? By wrapping boilerplate codes into several functions that eventually

becomes the vocabulary of the programmers.

Page 14: Functional programming (Let's fall back in love with Programming)

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

Page 15: Functional programming (Let's fall back in love with Programming)

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();

Page 16: Functional programming (Let's fall back in love with Programming)

DemoLet’s see some example using LINQ

Page 17: Functional programming (Let's fall back in love with Programming)

Now little bit of theoryJust enough so that you see the point

Page 18: Functional programming (Let's fall back in love with Programming)

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

Page 19: Functional programming (Let's fall back in love with Programming)

You have been already doing it

Excel Formulae

=SUM(AVERAGE(A1:A10))

LINUX/Powershell Pipes

ls | sort –r

Get-Process | Sort-Object id

Page 20: Functional programming (Let's fall back in love with Programming)

Function Types

Statistical

Gives a statistic of a collection.

Generators

Generates something

Filters

Filters a collection

Projectors

Projects a collection as something

Page 21: Functional programming (Let's fall back in love with Programming)

T [ ]

Generators

Generates something out of nothing.

Page 22: Functional programming (Let's fall back in love with Programming)

Filters

T [ ]T [ ]

T

Runs a predicate/filter for all elements of the given collection and

returns one or more matching entries.

Page 23: Functional programming (Let's fall back in love with Programming)

Projectors

U [ ]T [ ]

T and U can be same

Projects a list to another of same or other type using a mapping

function

Page 24: Functional programming (Let's fall back in love with Programming)

Statistical

T [ ] Boolean

T [ ] Numeric

Finds some statistical values for the given collection.

Page 25: Functional programming (Let's fall back in love with Programming)

Func<> and Action<> (C# Specific)

What type is this function ?

Page 26: Functional programming (Let's fall back in love with Programming)

Loop to LINQ

Page 27: Functional programming (Let's fall back in love with Programming)

Thank You So Much!! Send queries to [email protected]