SlideShare una empresa de Scribd logo
1 de 84
@pcameronpresley
Level Up on FP
Rebuilding LINQ
Cameron Presley
Cameron@TheSoftwareMentor.com
@pcameronpresley 2
Hello!
@pcameronpresley
What is LINQ?
Fundamentals
Implementing the Trinity
3
4
@pcameronpresley 5
@pcameronpresley 6
Which Delivery Teams
Have More
Than 7 Members?
@pcameronpresley 7
@pcameronpresley 8
@pcameronpresley
“Set of operations that works on data
independent of the source
In addition, these operations are type safe,
allowing the compiler to catch your
mistakes sooner
9
@pcameronpresley
Why LINQ?
10
@pcameronpresley
What Makes LINQ Work?
1. Func
2. Generics
3. Extension Methods
11
@pcameronpresley
Getting Functional with Funcs!
12
@pcameronpresley
Background
◦ Introduced in C# 3.0
◦ Type that when invoked returns a value
◦ Requires 0 or more parameters
13
@pcameronpresley
@pcameronpresley
Func<DeliveryTeam, bool>
Func<DeliveryTeam, string>
@pcameronpresley 16
@pcameronpresley 17
@pcameronpresley
Writing General Code
Generics
18
@pcameronpresley
Background
◦ Introduced in C# 2.0
◦ Allows for type parameters for various
constructs
◦ Great for when the type doesn’t matter
for implementation
19
@pcameronpresley 20
@pcameronpresley
@pcameronpresley 22
@pcameronpresley
When Generics Make Sense
When you’re writing the third structure in a
row…
Logic is the same, only the types are
different
23
@pcameronpresley 24
Extending Type Functionality
Extension Methods
@pcameronpresley
Background
◦ Introduced in C# 3.0
◦ Extends functionality to types without
having to recompile or creating a new
type
◦ Great for when working with types you
don’t have access to
25
@pcameronpresley 26
@pcameronpresley 27
You Could Have
Invented LINQ!
@pcameronpresley
“The most common extension methods
are the LINQ standard query operators
that add query functionality to the
existing System.Collections.IEnumerable
and
System.Collections.Generic.IEnumerable
<T> types.
28
MS Docs on Extension Methods
29
@pcameronpresley
Design Guidelines
◦ Extend the IEnumerable type
◦ Same signatures of LINQ
◦ Same properties as LINQ
30
@pcameronpresley
Changing Types with Map
31
@pcameronpresley
Background
Changes a collection of one type to
another by using a mapper function
Maintains the length of the collection
32
@pcameronpresley 33
Given a collection of DeliveryTeams,
we want to see the names, so we write the following
@pcameronpresley 34
@pcameronpresley 35
Given a collection of DeliveryTeams,
we want to see the TeamLeads, so we write the following
@pcameronpresley 36
@pcameronpresley 37
@pcameronpresley
@pcameronpresley 39
Given a collection of DeliveryTeams,
we want to know the number of team members on each team
@pcameronpresley 40
@pcameronpresley 41
@pcameronpresley 42
@pcameronpresley 43
Any reason we need to constraint this to the DeliveryTeam type?
@pcameronpresley 44
Select Imp from GitHub
@pcameronpresley 45
Do we need to limit to
List<TSource> and List<TResult>?
@pcameronpresley
“Use Map to Transform the
Collection Into The Data You
Need
46
@pcameronpresley
Keeping What’s Needed with Keep
47
@pcameronpresley
Background
Returns a subset of a collection based on a
predicate
Length is between 0 and original length
Doesn’t return items that weren’t in the
list
Maintains order
48
49
Which DeliveryTeams have more than 7 team members?
50
Which DeliveryTeams have Dana as a TeamLead?
51
52
@pcameronpresley 53
@pcameronpresley 54
Any reason why this would be
limited to DeliveryTeam?
55
@pcameronpresley 56
Where Implementation from GitHub
@pcameronpresley
“Use Keep to Keep Data That
You Want
57
@pcameronpresley
Going from many to one with
Reduce
58
@pcameronpresley
Background
Takes a collection, an initial value, and a
combiner method, returns a single value
59
@pcameronpresley 60
How many team members do we have in total?
@pcameronpresley 61
@pcameronpresley 62
@pcameronpresley 63
@pcameronpresley 64
@pcameronpresley 65
Any reason why this should only
work on DeliveryTeam?
@pcameronpresley 66
@pcameronpresley 67
Aggregate Imp from GitHub
@pcameronpresley
“Use Reduce to Transform a
Collection to a Single Value by
First Identifying the Initial
Value, Followed by Writing
the Combining Logic
68
@pcameronpresley
Reducing LINQ to Reduce
69
No, Seriously…
@pcameronpresley
Breaking Down The Problem
◦ Determine the Initial Value
◦ Determine how to combine the initial
value with the next element
70
@pcameronpresley 71
What’s the initial value?
What should we return if the source is empty?
Enumerable<TResult>.Empty()
@pcameronpresley
Setting Up Map via Reduce
72
1. Covert item from T to TResult
2. Append the converted item to values
@pcameronpresley
Converting from T to TResult
73
@pcameronpresley
Appending to values
74
@pcameronpresley
Redoing Keep as Reduce
75
@pcameronpresley 76
What’s the initial value?
What should we return if the source is empty?
Enumerable<TSource>.Empty()
@pcameronpresley
Setting Up Keep via Reduce
77
1. Check if the item meets the criteria
2. If the item meets, append to values
%%
@pcameronpresley
Checking if an item is valid
78
@pcameronpresley
Appending to values
79
@pcameronpresley
What Did We Cover?
What is LINQ?
Fundamentals (Func, Generics, Extension
Methods)
Implementing Map, Keep, Reduce
80
@pcameronpresley
LINQ isn’t magic
81
@pcameronpresley
Composition of Funcs,
Generics, Extension Methods
82
@pcameronpresley
Building bigger programs
from smaller programs
83
@pcameronpresley
Questions?
Cameron@TheSoftwareMentor.com
84

Más contenido relacionado

La actualidad más candente (6)

Inflammation
Inflammation Inflammation
Inflammation
 
PNEUMONIA.pptx
PNEUMONIA.pptxPNEUMONIA.pptx
PNEUMONIA.pptx
 
Hemopoiesis
HemopoiesisHemopoiesis
Hemopoiesis
 
Cardiac system final
Cardiac system finalCardiac system final
Cardiac system final
 
lung abscess
lung abscesslung abscess
lung abscess
 
Lungabscess 171204080119
Lungabscess 171204080119Lungabscess 171204080119
Lungabscess 171204080119
 

Similar a Level Up Your Functional Programming Skills with LINQ

Computer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .pptComputer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .ppt
RedenOriola
 

Similar a Level Up Your Functional Programming Skills with LINQ (20)

Establishing a SOLID Foundation
Establishing a SOLID FoundationEstablishing a SOLID Foundation
Establishing a SOLID Foundation
 
Testing: ¿what, how, why?
Testing: ¿what, how, why?Testing: ¿what, how, why?
Testing: ¿what, how, why?
 
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...
 
The Engineer's Playbook: Starting a New Role
The Engineer's Playbook: Starting a New RoleThe Engineer's Playbook: Starting a New Role
The Engineer's Playbook: Starting a New Role
 
Learning postgresql
Learning postgresqlLearning postgresql
Learning postgresql
 
Distributed System explained (with Java Microservices)
Distributed System explained (with Java Microservices)Distributed System explained (with Java Microservices)
Distributed System explained (with Java Microservices)
 
Machine Learning Infrastructure
Machine Learning InfrastructureMachine Learning Infrastructure
Machine Learning Infrastructure
 
Deeplearning for industries | Data to Production
Deeplearning for industries | Data to ProductionDeeplearning for industries | Data to Production
Deeplearning for industries | Data to Production
 
Java On CRaC
Java On CRaCJava On CRaC
Java On CRaC
 
Back to Basics Webinar 3: Introduction to Replica Sets
Back to Basics Webinar 3: Introduction to Replica SetsBack to Basics Webinar 3: Introduction to Replica Sets
Back to Basics Webinar 3: Introduction to Replica Sets
 
Our Story With ClickHouse at seo.do
Our Story With ClickHouse at seo.doOur Story With ClickHouse at seo.do
Our Story With ClickHouse at seo.do
 
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Intro To C++ - Class #17: Pointers!, Objects Talking To Each OtherIntro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
 
Deep learning with kafka
Deep learning with kafkaDeep learning with kafka
Deep learning with kafka
 
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG Torino
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG TorinoDistributed Systems explained (with NodeJS) - Bruno Bossola, JUG Torino
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG Torino
 
Distributed System explained (with NodeJS) - Bruno Bossola - Codemotion Milan...
Distributed System explained (with NodeJS) - Bruno Bossola - Codemotion Milan...Distributed System explained (with NodeJS) - Bruno Bossola - Codemotion Milan...
Distributed System explained (with NodeJS) - Bruno Bossola - Codemotion Milan...
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 
FluentMigrator - Dayton .NET - July 2023
FluentMigrator - Dayton .NET - July 2023FluentMigrator - Dayton .NET - July 2023
FluentMigrator - Dayton .NET - July 2023
 
Computer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .pptComputer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .ppt
 
Automation for Anyone at Nutanix NEXT 2017 US
Automation for Anyone at Nutanix NEXT 2017 USAutomation for Anyone at Nutanix NEXT 2017 US
Automation for Anyone at Nutanix NEXT 2017 US
 
From Mess To Masterpiece - JFokus 2017
From Mess To Masterpiece - JFokus 2017From Mess To Masterpiece - JFokus 2017
From Mess To Masterpiece - JFokus 2017
 

Más de Cameron Presley

Más de Cameron Presley (8)

Taking a Gamble with Functional Domain Modeling
Taking a Gamble with Functional Domain ModelingTaking a Gamble with Functional Domain Modeling
Taking a Gamble with Functional Domain Modeling
 
Functional Programming Through Construction : First Principles
Functional Programming Through Construction : First PrinciplesFunctional Programming Through Construction : First Principles
Functional Programming Through Construction : First Principles
 
How Functional Programming Made Me a Better Developer
How Functional Programming Made Me a Better DeveloperHow Functional Programming Made Me a Better Developer
How Functional Programming Made Me a Better Developer
 
How to Have Code Reviews That Developers Actually Want
How to Have Code Reviews That Developers Actually WantHow to Have Code Reviews That Developers Actually Want
How to Have Code Reviews That Developers Actually Want
 
Making the Unstable Stable - An Intro To Testing
Making the Unstable Stable - An Intro To TestingMaking the Unstable Stable - An Intro To Testing
Making the Unstable Stable - An Intro To Testing
 
Indy Code - Taking a Gamble With F#: Implementing Blackjack
Indy Code - Taking a Gamble With F#: Implementing BlackjackIndy Code - Taking a Gamble With F#: Implementing Blackjack
Indy Code - Taking a Gamble With F#: Implementing Blackjack
 
How Functional Programming Made Me A Better Developer
How Functional Programming Made Me A Better DeveloperHow Functional Programming Made Me A Better Developer
How Functional Programming Made Me A Better Developer
 
Establishing a SOLID Foundation - An Intro to Software Design
Establishing a SOLID Foundation - An Intro to Software DesignEstablishing a SOLID Foundation - An Intro to Software Design
Establishing a SOLID Foundation - An Intro to Software Design
 

Último

Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Último (20)

%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 

Level Up Your Functional Programming Skills with LINQ