0% found this document useful (0 votes)
3 views22 pages

Lecture 6 - Functional Programming

Functional programming is a declarative paradigm that emphasizes building programs by composing pure functions, which are deterministic and free from side effects. It promotes the use of immutable types and expressions over declarations to enhance code clarity and maintainability. The document also discusses concepts like functional composition, higher-order functions, and the use of LINQ in a functional context.

Uploaded by

m09221011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Lecture 6 - Functional Programming

Functional programming is a declarative paradigm that emphasizes building programs by composing pure functions, which are deterministic and free from side effects. It promotes the use of immutable types and expressions over declarations to enhance code clarity and maintainability. The document also discusses concepts like functional composition, higher-order functions, and the use of LINQ in a functional context.

Uploaded by

m09221011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Functional Programming

Abdulmottaleb Elabour

1
What Does Functional Programming Mean?
• Functional programming is a declarative programming paradigm. In this paradigm, programs are built by
composing functions.
• Function definitions are expression trees that map values to other values, instead of a sequence of
imperative instructions that update the program’s execution state.
• In simple terms, programming in a functional style allows developers to create software with declarative
code through small functions combined in a modular way, which makes the code cohesive, elegant and
easy to understand.
• Pure functions are a subset of functional programming that treats all functions as deterministic math
functions. So, when a pure function is called with some given arguments, it will always return the same
result and cannot be affected by any mutable state or other side effects.

2
‫عبد المطلب العبور‬
What Are Pure Functions?
• A pure function is a function whose output depends only on the arguments passed to it.
• If a pure function is invoked twice using the same input values, the result obtained will be the same
output for both cases.

3
‫عبد المطلب العبور‬
Advantages of Using Pure Functions
• Better understanding of the code
According to Robert Martin’s Clean Code book, the proportion of time spent reading code is much higher than
writing it, so if we have code that is easy to understand, the time spent on maintaining it tends to be very low.

• Easy debugging
In pure functions, we don’t have complexities created in imperative code. So to debug pure functions, most of
the time it will only depend on observing the values passed and why the expected result is not occurring.

• Easy-to-test code
The vast majority of projects today require unit tests to be carried out to ensure they work. When we write
pure functions, we don’t have any external dependencies—that is, we don’t need any kind of mock or any
other hack.

4
‫عبد المطلب العبور‬
Using Immutable Types
• Immutable types are objects whose state cannot be changed after they are created. Any "modification"
creates a new object instead of changing the original one.

• Avoiding state change is essential for functional programming. To do this, an alternative is to create immutable
types, so we guarantee that their values are not modified during execution. So, if there is a need to modify
them, a new instance of it must be created.

5
‫عبد المطلب العبور‬
Using mutable Types
• The example below first demonstrates the use of the imperative approach where the values of properties of a
mutable type are changed during execution.

6
‫عبد المطلب العبور‬
Immutable example
we created an immutable type with a method that returns a new instance instead of modifying it.

7
‫عبد المطلب العبور‬
Avoiding the Use of Declarations
Whenever possible, use expressions instead of declarations. In addition to making the code easier to interpret,
expressions are much faster than declarations and also avoid code complexity.

8
‫عبد المطلب العبور‬
Imperative VS Declarative Output:
39
26
111
1-
6
6-
15

9
‫عبد المطلب العبور‬
Imperative VS Declarative Output:
39
26
111
1-
6
6-
15

10
‫عبد المطلب العبور‬
Imperative VS Declarative
Output:
39
26
111
1-
6
6-
15

11
‫عبد المطلب العبور‬
LINQ in Functional Perspective

12
‫عبد المطلب العبور‬
LINQ in Functional Perspective
Numbers AddOne Square SubtractTen

6 7 49 39

5 6 36 26

10 11 121 111

2 3 9 -1

3 4 16 6

1 2 4 -6

4 5 25 15

13
‫عبد المطلب العبور‬
Data pipelines and composability
LINQ to Objects is built on the concept of a data pipeline. We start with a data source which implements
IEnumerable, and chain together lots of operations, each one acting on the result of the previous one and
returning another IEnumerable - although it could be one with a different type of result. This act of
chaining small operations together to form one large operation is called composition.

14
‫عبد المطلب العبور‬
Data pipelines and composability
Numbers AddOne Square SubtractTen

6 7 49 39

5 6 36 26

10 11 121 111

2 3 9 -1

3 4 16 6

1 2 4 -6

4 5 25 15

15
‫عبد المطلب العبور‬
Functional Composition
• In computer science, function composition is an act or mechanism to combine
simple functions to build more complicated ones. Like the usual composition of
functions in mathematics, the result of each function is passed as the argument of
the next.

16
‫عبد المطلب العبور‬
Functional Composition

17
‫عبد المطلب العبور‬
Functional Composition

Output:
39
26
111
1-
6
6-
15

18
‫عبد المطلب العبور‬
Higher order functions
• Treating functions as regular objects enables us to use them as arguments and results of other
functions. Functions that handle other functions are called higher order functions.

19
‫عبد المطلب العبور‬
Higher order functions Example

Output:
36
26
36
26
36
26

20
‫عبد المطلب العبور‬
Function Returns Function

Output:
90

21
‫عبد المطلب العبور‬
Function Returns Function

Output:
90

22
‫عبد المطلب العبور‬

You might also like