4. DISCLAIMER
I’M NOT AN EXPERT IN THIS
TOPIC!
But I will try to show you what I have learnt
5. Agenda
1. Introduction
a) What is functional programming?
b) Separation between functions and arguments (data)
c) Benefits of Functional Programming
2. Functional programming in C#
a) Data Objects
b) Honest functions
c) Tuple and ValueTuple
d) Option<T>
e) LINQ
7. What is Functional Programming?
• Functional programming is a paradigm which concentrates on computing
results rather than on performing actions.
8. What is Functional Programming?
• Functional programming is a paradigm which concentrates on computing
results rather than on performing actions.
• Functional programming is programming with mathematical
functions - used functions should always provide the same results for the
same input.
9. What is Functional Programming?
• Functional programming is a paradigm which concentrates on computing
results rather than on performing actions.
• Functional programming is programming with mathematical
functions - used functions should always provide the same results for the
same input.
• At very high level, it's a programming style that emphasizes functions
while avoiding state mutation.
10. What is Functional Programming?
• Functional programming is a paradigm which concentrates on computing
results rather than on performing actions.
• Functional programming is programming with mathematical
functions - used functions should always provide the same results for the
same input
• At very high level, it's a programming style that emphasizes functions
while avoiding state mutation.
• Functional programming is a style that treads computation as the
evaluation of mathematical functions and avoids changing-state and
mutable data
11. Separation between functions and arguments
(data)
• In Object Oriented Programming, state (data) and methods
(functions) are bounded in objects
12. Separation between functions and arguments
(data)
• In Object Oriented Programming, state (data) and methods
(functions) are bounded in objects
• In functional programming, logic is encoded in functions and data is
captured with data objects, which are the input and output of
functions
13. Separation between functions and arguments
(data)
• In Object Oriented Programming, state (data) and methods
(functions) are bounded in objects
• In functional programming, logic is encoded in functions and data is
captured with data objects, which are the input and output of
functions
• Math functions are not class method. Math functions does not leave
any mark of it's exsistsence in outside world, while class methods may
change the state of an object.
18. Benefits of Functional Programming
• Tamed Side Effects
• Simple concurrency (no shared variables)
• Straightforward unit testing
• Different point of view for looking at problems
26. Why use more specyfic data types?
• Primitive types are often not specific enough - for example, if you take
age, int can be -1000 as well as 1000.
27. Why use more specyfic data types?
• Primitive types are often not specific enough - for example, if you take
age, int can be -1000 as well as 1000.
• You’ll have to write additional unit tests for the cases in which
validation fails.
28. Why use more specyfic data types?
• Primitive types are often not specific enough - for example, if you take
age, int can be -1000 as well as 1000.
• You’ll have to write additional unit tests for the cases in which
validation fails.
• There are a few other areas of the application where an age is
expected, so you’re probably going to need the same validation in
those places. This will cause duplication.
37. Honest functions
• Honest function - it always honors signature. There is no other
possible outcome than defined (also no error throw). It just maps
each element of domain to an element of the codomain. Function is
honest if ts behavior can be predicted by its signature. No throwing
exceptions, and no null return values.
38. Honest functions
• Honest function - it always honors signature. There is no other
possible outcome than defined (also no error throw). It just maps
each element of domain to an element of the codomain. Function is
honest if ts behavior can be predicted by its signature. No throwing
exceptions, and no null return values.
• Dishonest function - give ma an int, and I may return a Risk.
63. Summary
• Data Objects instead of Primitive Types
• Honest functions to have output for every possible input
• ValueTuple to aggregate Data Objects
• Option<T> to avoid nulls
• LINQ instead of foreach
66. Bibliography (Pluralsight)
• Functional Programming with C# (Dave Fencher) (2015)
• Applying Functional Principles in C# (Vladimir Khorikov)
(2016)
• Making Your C# Code More Functional (Zoran Horvat)
(2018)
67. Bibliography (Presentations)
• Railway oriented programming: Error handling in functional languages
by Scott Wlaschin (link) (2014)
• Functional Programming in C# (link) (2018)