Functional Programming is getting a lot of attention nowadays. Even every mainstream language is now supporting functional style i.e Java, C++, C# etc. Basically, it is one of the paradigms of writing code where we write less code and do more.
Exploring the Real Power of Functional Programming
1. Exploring the Real Power of
Functional Programming
Presented By:
Mahesh Chand (Software Consultant)
Divya Dua (Software Consultant)
2. Agenda
❏ Conclusion
❏ The Free Lunch Is Over
❏ The Downfall of Imperative Programming
❏ Rise of Functional Programming
❏ Imperative Style vs Declarative Style
❏ Pure Function
❏ Functional Composition
❏ Lazy Evaluation
3. The Free Lunch Is Over
Moore’s Law: The number of transistors are getting double in every 18 months.
Why don’t we have 10GHz Processor today ?
Chip designers are under so much pressure to deliver ever-faster CPUs that they’ll
risk changing the meaning of your program, and possibly break it, in order to make
it run faster.
What is free lunch actually ?
Programmers have not really had to worry much about performance or concurrency
because of moore’s law.
Application Developers did not need to invest in performance tuning and enjoyed
the free lunch from hardware improvement.
4.
5. The Downfall of Imperative Programming
The nature of imperative coding is assignment programming.
Assignment programming leads to side effects.
When a function has side effect, you need another method to undo the side effect
For example, Opening a file to read, to undo the side effect, there is need to write
close method etc
An application which is working fine on single core processor but the moment it
runs on multicore system it starts behaving weird and ends up being broken.
Another problem arises from having mutable states. It is extremely difficult to work
with mutability especially when multiple threads start sharing it.
So, what’s the solution ?
8. Rise of Functional Programming
Functional programming is an assignment less programming.
It is an inherently thread-safe. Silver bullet for leverage the multi-core processors.
It is like writing unix pipelines i.e a unix pipeline starts with a source of data — the
who command — and then transforms that data with one or more func-
tions. Data flows through the pipeline in only one direction, from one
function to the next, and data is never modified, it’s only transformed from
one format to another.
9. Imperative vs Declarative
● Hard to maintain and compose ● Easy to maintain and compose
● Need to tell what and how to do. ● Need to tell only what to do.
● Code is too verbose. ● Code is concise, not terse..
● Favours mutability. ● No explicit mutation.
● Sometimes, hard to
reason
● Easier to reason.
● Exhibit primitive
obsession.
● Highly expressive.
10. Pure Function
Pure function produces the same output no matter how many time you make a call
to it as long as you provide the same set of inputs.
Advantages of Pure functions:
● Easier to test
● Memoizable
● Referential transparency
● Easy to compose
● Easy to understand
11. Functional Composition
❏ One of the key features of Functional Programming.
❏ It is an act or mechanism to combine simple functions to build more
complicated ones.
❏ The result of each function is passed as the argument of the next function, and
the result of last one is the result of the whole.
12. ❏ Functional Composition makes the code highly expressive, highly cohesive,
concise and easier to understand.
● Ordering using andThen: f(x) andThen g(x) = g(f(x))
● Ordering using compose: f(x) compose g(x) = f(g(x))
❏ You can make use of in-built functions andThen and compose to
combine multiple functions.
❏ Functional composition, as concept, refers to how we can compose
functions to form a pipeline of operations.
14. ❏ Functional Composition makes code more expressive and lazy evaluation
adds efficiency to it.
❏ If the value is never needed during the execution of the program, the evaluation
of the expression can be totally skipped.
❏ Efficiency is achieved not just by running things faster, but by avoiding things
that shouldn't be done in the first place.
❏ When used in the context of functional programming, the word lazy may be
pronounced as efficient.
15. Conclusion
❖ Functional Programming is like an old wine in new bottle. It is healthy and
effective for multithreaded applications.
❖ Functional Programming is not a better model because it makes concurrency
easier; It makes concurrency easier because it is a better model.
❖ Using it, we can write code which is concise, highly expressive, uses no
garbage variables and has no explicit mutation.
❖ Object Oriented Programming makes the code understandable by
encapsulating moving parts while functional programming makes the code
understandable by minimizing the moving parts.
16. References
● The Free Lunch Is Over
● Robert C Martin - Functional Programming; What? Why? When?
● Functional Programming Basics
● Benefits of Pure Funcitons
● Lazy Evaluation
● Github URL - functional-programming