SlideShare a Scribd company logo
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 library
Rahul Sharma
 
Practical cats
Practical catsPractical cats
Practical cats
Raymond Tay
 
01. haskell introduction
01. haskell introduction01. haskell introduction
01. haskell introduction
Sebastian Rettig
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
Sunil OS
 
sets and maps
 sets and maps sets and maps
sets and maps
Rajkattamuri
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-library
Hariz Mustafa
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
Michael Heron
 
C++ STL 概觀
C++ STL 概觀C++ STL 概觀
C++ STL 概觀
PingLun Liao
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
Johan Tibell
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
Micheal Ogundero
 
Extensible Effects in Dotty
Extensible Effects in DottyExtensible Effects in Dotty
Extensible Effects in Dotty
Sanshiro Yoshida
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
Edureka!
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
Tomer 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
 
Java Script Introduction
Java Script IntroductionJava Script Introduction
Java Script Introduction
jason hu 金良胡
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
Muhammad Hammad Waseem
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
GauravPatil318
 
Advance xpath
Advance xpathAdvance xpath
Advance xpath
Suresh G
 
Python standard data types
Python standard data typesPython standard data types
Python standard data types
Learnbay Datascience
 
Hackersnl
HackersnlHackersnl
Hackersnl
chriseidhof
 

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 quiz
Sebastian Rettig
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
Vincent Pradeilles
 
iPython
iPythoniPython
iPython
Aman Lalpuria
 
02. haskell motivation
02. haskell motivation02. haskell motivation
02. haskell motivation
Sebastian Rettig
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
Saugat 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, ML
Simone 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 181
Mahmoud Samir Fayed
 
C# programming
C# programming C# programming
C# programming
umesh patil
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
VisnuDharsini
 
Functional Programming and Ruby
Functional Programming and RubyFunctional Programming and Ruby
Functional Programming and Ruby
Pat Shaughnessy
 
Fp java8
Fp java8Fp java8
Fp java8
Yanai Franchi
 
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
djspiewak
 
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
Mahmoud Samir Fayed
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
Meetu Maltiar
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
Knoldus Inc.
 
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
Franklin 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)
 
CppOperators.ppt
CppOperators.pptCppOperators.ppt
CppOperators.ppt
TejasriKondisetti
 

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
 
Function therory
Function theroryFunction therory
Function therory
Tuomas Hietanen
 
The Pain Points of C#
The Pain Points of C#The Pain Points of C#
The Pain Points of C#
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
 
Linq in practice
Linq in practiceLinq in practice
Linq in practice
Tuomas Hietanen
 
Using f# project from c#
Using f# project from c#Using f# project from c#
Using f# project from c#
Tuomas Hietanen
 
Funktioteoriaa
FunktioteoriaaFunktioteoriaa
Funktioteoriaa
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
 
F# ja C# yhteiskäyttö
F# ja C# yhteiskäyttöF# ja C# yhteiskäyttö
F# ja C# yhteiskäyttö
Tuomas Hietanen
 
C# nykyiset kipupisteet
C# nykyiset kipupisteetC# nykyiset kipupisteet
C# nykyiset kipupisteet
Tuomas Hietanen
 
LINQ käytännössä
LINQ käytännössäLINQ käytännössä
LINQ käytännössä
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

GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 

Recently uploaded (20)

GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 

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