SlideShare a Scribd company logo
1 of 69
Functional programming in
C#
Tadeusz Balcer
DISCLAIMER
DISCLAIMER
I’M NOT AN EXPERT IN THIS
TOPIC!
DISCLAIMER
I’M NOT AN EXPERT IN THIS
TOPIC!
But I will try to show you what I have learnt 
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
What is Functional Programming?
What is Functional Programming?
• Functional programming is a paradigm which concentrates on computing
results rather than on performing actions.
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.
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.
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
Separation between functions and arguments
(data)
• In Object Oriented Programming, state (data) and methods
(functions) are bounded in objects
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
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.
Functions
Methods
Functional programming in practice
Aggregation of functions
Benefits of Functional Programming
• Tamed Side Effects
• Simple concurrency (no shared variables)
• Straightforward unit testing
• Different point of view for looking at problems
Benefits of Functional Programming
Benefits of Functional Programming
Functional programming in
C#
Making code more
functional
Use more specyfic
arguments and results
Data Objects instead of Primitive Types
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.
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.
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.
Data Objects
Data Objects – interaction with primitive
types
Data Objects – interaction with primitive
types
Data Objects – implicit/explicit operators
Data Objects – implicite/explicit operators
Honest functions
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.
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.
Honest functions
Dishonest functions
Honest functions
Honest functions
Honest functions
Honest functions
DISHONEST SIGNATURE HONEST SIGNATURE
Composing values
Composing values with tuples
Tuple
TupleValue
Avoiding nulls
Avoiding null bomb – Option<T>
Avoiding null bomb – Option<T>
Avoiding null bomb – Option<T>
Avoiding null bomb – Option<T>
LINQ
LINQ
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
Bibliography
• https://github.com/
louthy/language-
ext/wiki
Bibliography
• Functional Programming in C#: How to
write better C# code (2017)
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)
Bibliography (Presentations)
• Railway oriented programming: Error handling in functional languages
by Scott Wlaschin (link) (2014)
• Functional Programming in C# (link) (2018)
Questions?
Thank you!

More Related Content

What's hot

Protein secondary structure prediction by a neural network architecture with...
Protein secondary structure prediction by a neural network  architecture with...Protein secondary structure prediction by a neural network  architecture with...
Protein secondary structure prediction by a neural network architecture with...IJECEIAES
 
Career oppurtunities in the field of Bioinformatics
Career oppurtunities in the field of BioinformaticsCareer oppurtunities in the field of Bioinformatics
Career oppurtunities in the field of BioinformaticsShikha Thakur
 
hg19 (GRCh37) vs. hg38 (GRCh38)
hg19 (GRCh37) vs. hg38 (GRCh38)hg19 (GRCh37) vs. hg38 (GRCh38)
hg19 (GRCh37) vs. hg38 (GRCh38)Shaojun Xie
 
pET vector. Plasmid for Expression by T7 RNA Polymerase.
pET vector. Plasmid for Expression by T7 RNA Polymerase.pET vector. Plasmid for Expression by T7 RNA Polymerase.
pET vector. Plasmid for Expression by T7 RNA Polymerase.MuhammadMujahid58
 
Tools for modeling protein 3 d structure
Tools for modeling protein 3 d structureTools for modeling protein 3 d structure
Tools for modeling protein 3 d structurePriyanka Kashyap
 
Ncbi basic intro_v_pitt_kent_osu
Ncbi basic intro_v_pitt_kent_osuNcbi basic intro_v_pitt_kent_osu
Ncbi basic intro_v_pitt_kent_osuBen Busby
 

What's hot (20)

Protein Databases
Protein DatabasesProtein Databases
Protein Databases
 
Intro to databases
Intro to databasesIntro to databases
Intro to databases
 
Protein secondary structure prediction by a neural network architecture with...
Protein secondary structure prediction by a neural network  architecture with...Protein secondary structure prediction by a neural network  architecture with...
Protein secondary structure prediction by a neural network architecture with...
 
Prosite
PrositeProsite
Prosite
 
Career oppurtunities in the field of Bioinformatics
Career oppurtunities in the field of BioinformaticsCareer oppurtunities in the field of Bioinformatics
Career oppurtunities in the field of Bioinformatics
 
Dna assembly techniques
Dna assembly techniquesDna assembly techniques
Dna assembly techniques
 
DNA Nanotechnology: Concept and its Applications
DNA Nanotechnology: Concept and its ApplicationsDNA Nanotechnology: Concept and its Applications
DNA Nanotechnology: Concept and its Applications
 
Gene Expression Omnibus (GEO)
Gene Expression Omnibus (GEO)Gene Expression Omnibus (GEO)
Gene Expression Omnibus (GEO)
 
hg19 (GRCh37) vs. hg38 (GRCh38)
hg19 (GRCh37) vs. hg38 (GRCh38)hg19 (GRCh37) vs. hg38 (GRCh38)
hg19 (GRCh37) vs. hg38 (GRCh38)
 
Biological databases.pptx
Biological databases.pptxBiological databases.pptx
Biological databases.pptx
 
pET vector. Plasmid for Expression by T7 RNA Polymerase.
pET vector. Plasmid for Expression by T7 RNA Polymerase.pET vector. Plasmid for Expression by T7 RNA Polymerase.
pET vector. Plasmid for Expression by T7 RNA Polymerase.
 
Sequence Analysis
Sequence AnalysisSequence Analysis
Sequence Analysis
 
Kegg databse
Kegg databseKegg databse
Kegg databse
 
Biological databases
Biological databasesBiological databases
Biological databases
 
Pac vector ppt
Pac vector ppt Pac vector ppt
Pac vector ppt
 
Biological databases
Biological databasesBiological databases
Biological databases
 
Tools for modeling protein 3 d structure
Tools for modeling protein 3 d structureTools for modeling protein 3 d structure
Tools for modeling protein 3 d structure
 
NCBI
NCBINCBI
NCBI
 
Bio materials
Bio materialsBio materials
Bio materials
 
Ncbi basic intro_v_pitt_kent_osu
Ncbi basic intro_v_pitt_kent_osuNcbi basic intro_v_pitt_kent_osu
Ncbi basic intro_v_pitt_kent_osu
 

Similar to Functional Programming in C#

Functional Programming - Worth the Effort
Functional Programming - Worth the EffortFunctional Programming - Worth the Effort
Functional Programming - Worth the EffortBoldRadius Solutions
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in RubyAlex Teut
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean CodingMetin Ogurlu
 
Functional Swift
Functional SwiftFunctional Swift
Functional SwiftGeison Goes
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptxKarthickT28
 
XII Computer Science- Chapter 1-Function
XII  Computer Science- Chapter 1-FunctionXII  Computer Science- Chapter 1-Function
XII Computer Science- Chapter 1-FunctionPrem Joel
 
Intro to Functional Programming @ Scala Montreal
Intro to Functional Programming @ Scala MontrealIntro to Functional Programming @ Scala Montreal
Intro to Functional Programming @ Scala Montrealfelixtrepanier
 
Functional solid
Functional solidFunctional solid
Functional solidMatt Stine
 
Math with .NET for you and Azure
Math with .NET for you and AzureMath with .NET for you and Azure
Math with .NET for you and AzureMarco Parenzan
 
A Theory of Functional Programming LambdUp
A Theory of Functional Programming LambdUpA Theory of Functional Programming LambdUp
A Theory of Functional Programming LambdUpEric Normand
 
Python functional programming
Python functional programmingPython functional programming
Python functional programmingGeison Goes
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Ovidiu Farauanu
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsRajendran
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]Rome468
 

Similar to Functional Programming in C# (20)

RAJAT PROJECT.pptx
RAJAT PROJECT.pptxRAJAT PROJECT.pptx
RAJAT PROJECT.pptx
 
Functional Programming - Worth the Effort
Functional Programming - Worth the EffortFunctional Programming - Worth the Effort
Functional Programming - Worth the Effort
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
 
Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
Functional Swift
Functional SwiftFunctional Swift
Functional Swift
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptx
 
XII Computer Science- Chapter 1-Function
XII  Computer Science- Chapter 1-FunctionXII  Computer Science- Chapter 1-Function
XII Computer Science- Chapter 1-Function
 
Intro to Functional Programming @ Scala Montreal
Intro to Functional Programming @ Scala MontrealIntro to Functional Programming @ Scala Montreal
Intro to Functional Programming @ Scala Montreal
 
Functional Go
Functional GoFunctional Go
Functional Go
 
Functional solid
Functional solidFunctional solid
Functional solid
 
Math with .NET for you and Azure
Math with .NET for you and AzureMath with .NET for you and Azure
Math with .NET for you and Azure
 
A Theory of Functional Programming LambdUp
A Theory of Functional Programming LambdUpA Theory of Functional Programming LambdUp
A Theory of Functional Programming LambdUp
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
 
Lecture 1.pptx
Lecture 1.pptxLecture 1.pptx
Lecture 1.pptx
 

Recently uploaded

Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 

Recently uploaded (20)

Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 

Functional Programming in C#

  • 3. DISCLAIMER I’M NOT AN EXPERT IN THIS TOPIC!
  • 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
  • 6. What is Functional Programming?
  • 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
  • 19. Benefits of Functional Programming
  • 20. Benefits of Functional Programming
  • 22.
  • 24.
  • 25. Use more specyfic arguments and results Data Objects instead of Primitive Types
  • 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.
  • 30.
  • 31.
  • 32. Data Objects – interaction with primitive types
  • 33. Data Objects – interaction with primitive types
  • 34. Data Objects – implicit/explicit operators
  • 35. Data Objects – implicite/explicit operators
  • 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.
  • 47. Tuple
  • 50.
  • 51. Avoiding null bomb – Option<T>
  • 52. Avoiding null bomb – Option<T>
  • 53.
  • 54. Avoiding null bomb – Option<T>
  • 55. Avoiding null bomb – Option<T>
  • 56. LINQ
  • 57.
  • 58.
  • 59. LINQ
  • 60.
  • 61.
  • 62.
  • 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
  • 65. Bibliography • Functional Programming in C#: How to write better C# code (2017)
  • 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)