SlideShare a Scribd company logo
1 of 12
Download to read offline
Coding with LINQ,
Patterns & Practices
 LINQ & Functional ways




                          © Tuomas Hietanen (LGPLv3)
LINQ (Monads)
• Two types of functions
  – Queries and Aggreagtes
                                                  AGGREGATE:
                                                  - returns result
   (Wrapper)           Wrapped type,
                       In this case
                       IEnumerable<T>




               QUERY:
               - returns another container of the same type
                              © Tuomas Hietanen
LINQ SELECT = LIST.MAP
                (Query)
                   MAP function:




IEnumerable<   >                            IEnumerable<   >



                        © Tuomas Hietanen
LINQ WHERE= LIST.FILTER
               (Query)
                   FILTER function:




IEnumerable<   >                             IEnumerable<   >



                         © Tuomas Hietanen
LINQ AGGREGATE= LIST.FOLD
           (Aggregate)
                   FOLD function:




IEnumerable<   >



                        © Tuomas Hietanen
Other Aggregates
             (to make things easier)

Function        Aggregate
                function
LIST.SUM        Add every
                to
LIST.COUNT      Add (+1)
                to
LIST.MAX        Select bigger of
                     or
...




                           © Tuomas Hietanen
LINQ SELECTMANY
- Not pure, has many overrides
         SELECT function:




                                   IEnumerable<   >



               © Tuomas Hietanen
LINQ SELECTMANY
- Not pure, has many overrides

    SELECT function:




                       © Tuomas Hietanen
Old Legacy Way               New C#, LINQ                     F#
var result = new List<T>(); var result = from x in xs         let result =
foreach(var x in xs){                   select …                 List.map (fun x -> ...) xs
   result.Add(...);         //same as
   ...                      var result = xs.Select(x=>...);
}
foreach(var x in xs){        var result = from x in xs        let result =
   if(x=...){...}                        where …                 List.filter(fun x -> ...) xs
}                            //same as
                             var result = xs.Where(x=>...);
var result = new T();        //many ways, based on            //many ways, based on
foreach(var x in xs){        var result = xs.Aggregate(…);    let result =
   result = ...;                                                 List.fold (fun f -> ...) xs
   ...
}                                                             //also supports unfold

foreach(var x in xs){        var result = from x in xs        //SelectMany is not pure,
  foreach(var y in ...){                  from y in …         so depends on situation.
    ....                                  select …            E.g. List.collect, or seq {...}
  }                          //same as                        let result =
}                            var result =                        List.collect (fun x -> ...) xs
                                     xs.SelectMany(...);
                                       © Tuomas Hietanen
Do use lists!
• Don’t return Null. Prefer throw
  InvalidOperationException on error and
  Enumerable.Empty<T>() on case of empty list
• Don’t modify collections, do use new ones
    list.RemoveAt(0)
    newlist = list.Where(interestingItems => ...)
• Use IEnumerable<T> not List<T> or IList<T>
  – It is the baseclass and default for LINQ
  – You can always say .toList() to evaluate the lazy
    collection.

                        © Tuomas Hietanen
Safe class design, avoid side effects
• Don’t reuse variables
     x=x+1
     y=x+1
• Avoid global and class variables. Use method
  parameters
   – Better for function composition
• Don’t overcapuslate layers or class structures
   – No coding for fun: Focus on the functionality
• Declarative programming.
   – ”yield return” is ok in some cases, but not really
     declarative.
• Also old design practices do still apply (=> FxCop).
                           © Tuomas Hietanen
F# list vs seq
• List is F# default
   – Linked list
   – Good for recursive methods
• Seq is IEnumerable<T>
   – Lazy evaluation
   – Good for .NET interop




                       © Tuomas Hietanen

More Related Content

What's hot

Understanding the components of standard template library
Understanding the components of standard template libraryUnderstanding the components of standard template library
Understanding the components of standard template libraryRahul Sharma
 
Collections Framework
Collections FrameworkCollections Framework
Collections FrameworkSunil OS
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-libraryHariz Mustafa
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance HaskellJohan Tibell
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharpMicheal Ogundero
 
Extensible Effects in Dotty
Extensible Effects in DottyExtensible Effects in Dotty
Extensible Effects in DottySanshiro Yoshida
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaEdureka!
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!priort
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryGauravPatil318
 
Advance xpath
Advance xpathAdvance xpath
Advance xpathSuresh G
 

What's hot (20)

Understanding the components of standard template library
Understanding the components of standard template libraryUnderstanding the components of standard template library
Understanding the components of standard template library
 
Practical cats
Practical catsPractical cats
Practical cats
 
01. haskell introduction
01. haskell introduction01. haskell introduction
01. haskell introduction
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
sets and maps
 sets and maps sets and maps
sets and maps
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-library
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
C++ STL 概觀
C++ STL 概觀C++ STL 概觀
C++ STL 概觀
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
 
Extensible Effects in Dotty
Extensible Effects in DottyExtensible Effects in Dotty
Extensible Effects in Dotty
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
 
Java Script Introduction
Java Script IntroductionJava Script Introduction
Java Script Introduction
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Advance xpath
Advance xpathAdvance xpath
Advance xpath
 
Python standard data types
Python standard data typesPython standard data types
Python standard data types
 
Hackersnl
HackersnlHackersnl
Hackersnl
 

Similar to Coding with LINQ, Patterns & Practices

03. haskell refresher quiz
03. haskell refresher quiz03. haskell refresher quiz
03. haskell refresher quizSebastian Rettig
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in SwiftSaugat Gautam
 
On fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLOn fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLSimone Di Maulo
 
The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181Mahmoud Samir Fayed
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programmingVisnuDharsini
 
Functional Programming and Ruby
Functional Programming and RubyFunctional Programming and Ruby
Functional Programming and RubyPat Shaughnessy
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scaladjspiewak
 
The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88Mahmoud Samir Fayed
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsEelco Visser
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckFranklin Chen
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Harmeet Singh(Taara)
 

Similar to Coding with LINQ, Patterns & Practices (20)

03. haskell refresher quiz
03. haskell refresher quiz03. haskell refresher quiz
03. haskell refresher quiz
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
 
iPython
iPythoniPython
iPython
 
02. haskell motivation
02. haskell motivation02. haskell motivation
02. haskell motivation
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
 
On fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLOn fuctional programming, high order functions, ML
On fuctional programming, high order functions, ML
 
The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181
 
C# programming
C# programming C# programming
C# programming
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
 
Functional Programming and Ruby
Functional Programming and RubyFunctional Programming and Ruby
Functional Programming and Ruby
 
Fp java8
Fp java8Fp java8
Fp java8
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheck
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
 
CppOperators.ppt
CppOperators.pptCppOperators.ppt
CppOperators.ppt
 

More from Tuomas Hietanen

Blockchain (using NBitcoin and FSharp)
Blockchain (using NBitcoin and FSharp)Blockchain (using NBitcoin and FSharp)
Blockchain (using NBitcoin and FSharp)Tuomas Hietanen
 
Machine learning (using Accord.NET and FSharp)
Machine learning (using Accord.NET and FSharp)Machine learning (using Accord.NET and FSharp)
Machine learning (using Accord.NET and FSharp)Tuomas Hietanen
 
Possible FSharp Refactorings could be...
Possible FSharp Refactorings could be...Possible FSharp Refactorings could be...
Possible FSharp Refactorings could be...Tuomas Hietanen
 
Message passing & NoSQL (in English)
Message passing & NoSQL (in English)Message passing & NoSQL (in English)
Message passing & NoSQL (in English)Tuomas Hietanen
 
F# references (and some misc slides)
F# references (and some misc slides)F# references (and some misc slides)
F# references (and some misc slides)Tuomas Hietanen
 
Using f# project from c#
Using f# project from c#Using f# project from c#
Using f# project from c#Tuomas Hietanen
 
Pari sekalaista diaa ja F#-Referenssejä
Pari sekalaista diaa ja F#-ReferenssejäPari sekalaista diaa ja F#-Referenssejä
Pari sekalaista diaa ja F#-ReferenssejäTuomas Hietanen
 

More from Tuomas Hietanen (14)

Blockchain (using NBitcoin and FSharp)
Blockchain (using NBitcoin and FSharp)Blockchain (using NBitcoin and FSharp)
Blockchain (using NBitcoin and FSharp)
 
Machine learning (using Accord.NET and FSharp)
Machine learning (using Accord.NET and FSharp)Machine learning (using Accord.NET and FSharp)
Machine learning (using Accord.NET and FSharp)
 
Possible FSharp Refactorings could be...
Possible FSharp Refactorings could be...Possible FSharp Refactorings could be...
Possible FSharp Refactorings could be...
 
Message passing & NoSQL (in English)
Message passing & NoSQL (in English)Message passing & NoSQL (in English)
Message passing & NoSQL (in English)
 
Function therory
Function theroryFunction therory
Function therory
 
The Pain Points of C#
The Pain Points of C#The Pain Points of C#
The Pain Points of C#
 
F# references (and some misc slides)
F# references (and some misc slides)F# references (and some misc slides)
F# references (and some misc slides)
 
Linq in practice
Linq in practiceLinq in practice
Linq in practice
 
Using f# project from c#
Using f# project from c#Using f# project from c#
Using f# project from c#
 
Funktioteoriaa
FunktioteoriaaFunktioteoriaa
Funktioteoriaa
 
Pari sekalaista diaa ja F#-Referenssejä
Pari sekalaista diaa ja F#-ReferenssejäPari sekalaista diaa ja F#-Referenssejä
Pari sekalaista diaa ja F#-Referenssejä
 
F# ja C# yhteiskäyttö
F# ja C# yhteiskäyttöF# ja C# yhteiskäyttö
F# ja C# yhteiskäyttö
 
C# nykyiset kipupisteet
C# nykyiset kipupisteetC# nykyiset kipupisteet
C# nykyiset kipupisteet
 
LINQ käytännössä
LINQ käytännössäLINQ käytännössä
LINQ käytännössä
 

Recently uploaded

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

Coding with LINQ, Patterns & Practices

  • 1. Coding with LINQ, Patterns & Practices LINQ & Functional ways © Tuomas Hietanen (LGPLv3)
  • 2. LINQ (Monads) • Two types of functions – Queries and Aggreagtes AGGREGATE: - returns result (Wrapper) Wrapped type, In this case IEnumerable<T> QUERY: - returns another container of the same type © Tuomas Hietanen
  • 3. LINQ SELECT = LIST.MAP (Query) MAP function: IEnumerable< > IEnumerable< > © Tuomas Hietanen
  • 4. LINQ WHERE= LIST.FILTER (Query) FILTER function: IEnumerable< > IEnumerable< > © Tuomas Hietanen
  • 5. LINQ AGGREGATE= LIST.FOLD (Aggregate) FOLD function: IEnumerable< > © Tuomas Hietanen
  • 6. Other Aggregates (to make things easier) Function Aggregate function LIST.SUM Add every to LIST.COUNT Add (+1) to LIST.MAX Select bigger of or ... © Tuomas Hietanen
  • 7. LINQ SELECTMANY - Not pure, has many overrides SELECT function: IEnumerable< > © Tuomas Hietanen
  • 8. LINQ SELECTMANY - Not pure, has many overrides SELECT function: © Tuomas Hietanen
  • 9. Old Legacy Way New C#, LINQ F# var result = new List<T>(); var result = from x in xs let result = foreach(var x in xs){ select … List.map (fun x -> ...) xs result.Add(...); //same as ... var result = xs.Select(x=>...); } foreach(var x in xs){ var result = from x in xs let result = if(x=...){...} where … List.filter(fun x -> ...) xs } //same as var result = xs.Where(x=>...); var result = new T(); //many ways, based on //many ways, based on foreach(var x in xs){ var result = xs.Aggregate(…); let result = result = ...; List.fold (fun f -> ...) xs ... } //also supports unfold foreach(var x in xs){ var result = from x in xs //SelectMany is not pure, foreach(var y in ...){ from y in … so depends on situation. .... select … E.g. List.collect, or seq {...} } //same as let result = } var result = List.collect (fun x -> ...) xs xs.SelectMany(...); © Tuomas Hietanen
  • 10. Do use lists! • Don’t return Null. Prefer throw InvalidOperationException on error and Enumerable.Empty<T>() on case of empty list • Don’t modify collections, do use new ones list.RemoveAt(0) newlist = list.Where(interestingItems => ...) • Use IEnumerable<T> not List<T> or IList<T> – It is the baseclass and default for LINQ – You can always say .toList() to evaluate the lazy collection. © Tuomas Hietanen
  • 11. Safe class design, avoid side effects • Don’t reuse variables x=x+1 y=x+1 • Avoid global and class variables. Use method parameters – Better for function composition • Don’t overcapuslate layers or class structures – No coding for fun: Focus on the functionality • Declarative programming. – ”yield return” is ok in some cases, but not really declarative. • Also old design practices do still apply (=> FxCop). © Tuomas Hietanen
  • 12. F# list vs seq • List is F# default – Linked list – Good for recursive methods • Seq is IEnumerable<T> – Lazy evaluation – Good for .NET interop © Tuomas Hietanen