05 functional programming

48
Functional Programming Ideas in C#

Upload: victor-matyushevskyy

Post on 10-May-2015

1.395 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: 05 functional programming

Functional Programming Ideas in C#

Page 2: 05 functional programming

What’s wrong with imperative code?

Page 3: 05 functional programming

What’s wrong with imperative code?

Page 4: 05 functional programming

What’s wrong with imperative code?

Page 5: 05 functional programming

What’s wrong with imperative code?

• It is repetitive• It is lengthy• (higher chances to introduce bugs)• (harder to read)• It describes what to do and how• (mixing low-level and high-level

concepts)

Page 6: 05 functional programming

Let’s try to solve these problems

Page 7: 05 functional programming

Delegate declaration

Page 8: 05 functional programming

Invocation

Page 9: 05 functional programming

Invocation (without noise)

Page 10: 05 functional programming

Delegates are immutable

You can only change the delegate variable

Page 11: 05 functional programming

Delegate knows its target

Page 12: 05 functional programming

Look, we manipulate code

as if it was data!

Nothing new though(we had interfaces for

ages)

Page 13: 05 functional programming

So we can • Store it in variable• Store it in a field or property• Pass it as a method parameter• Return it as a method result

Page 14: 05 functional programming

Functions (delegates) are first class citizens

Both pure and impure

Page 15: 05 functional programming

Pure vs side-effects

• Pure functions:–Maht.Sin(X)– String.Length– List<T>.IndexOf(T)

• Functions with side effects:– List<T>.Add(T)– DateTime.UtcNow– Console.WriteLine()

Page 16: 05 functional programming

Questions so far?

Page 17: 05 functional programming

Consider the problem

• Initiate processing• When some condition occurs, we

want to execute some code

Page 18: 05 functional programming

Pass delegates

Page 19: 05 functional programming

Call event

Page 20: 05 functional programming

Event syntax

Page 21: 05 functional programming

Nothing really complicated so far

Let’s go deeper

Page 22: 05 functional programming
Page 23: 05 functional programming

Separate method -- not needed

Page 24: 05 functional programming

Lambda expressions(lambda functions)

Page 25: 05 functional programming

Return value from delegate

Page 26: 05 functional programming

Let’s recall generics!

Page 27: 05 functional programming

Questions so far?

Page 28: 05 functional programming

How to make it more flexible?

Page 29: 05 functional programming

This is called closure (lexical closure)

The function captures variables from the scope

Page 30: 05 functional programming

Prefix

Page 31: 05 functional programming

Pitfall

Page 32: 05 functional programming

Function can even capture itself

Page 33: 05 functional programming

Function factory

Page 34: 05 functional programming

Generate IDs

Page 35: 05 functional programming

We’ve looked at examples of

higher-order functions

Theytake one or more functions as an input

or return a function

Page 36: 05 functional programming

Be comfortable with creating and passing lambdas

Understand closures

This is very powerful and widely used idiom

Page 37: 05 functional programming

Let’s improve imperative code

Page 38: 05 functional programming

Find element

Page 39: 05 functional programming

Find element

Page 40: 05 functional programming

Filter

Page 41: 05 functional programming

Filter

Page 42: 05 functional programming

Currying

Haskell Curry

Transform f: (X * Y) -> Z

into f: X -> (Y -> Z)

Input: Function with N parameters

Output: chain of N functions with 1 parameter each

Page 43: 05 functional programming

Currying

Page 44: 05 functional programming

How to compute Fibonacci(N)

What’s wrong with this?

It is exponential(slow)

link

Page 45: 05 functional programming

Memoization

Page 46: 05 functional programming

Memoization

Page 47: 05 functional programming

Main ideas

• Functions as first class citizens• Compose complex functions from

simple• Create specific functions out of

generic ones• More generic and reusable code with

higher order functions• For less code and less bugs

Page 48: 05 functional programming

Questions?