esoteric linq and structural madness
DESCRIPTION
TRANSCRIPT
![Page 1: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/1.jpg)
Esoteric LINQAND STRUCTURAL MADNESS
![Page 2: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/2.jpg)
I Think Therefore I Am
Chris Eargle
Mad Scientist
Telerik Evangelist
C# MVP
INETA Board of Directors
Contacts kodefuguru.com
callto://kodefuguru
![Page 3: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/3.jpg)
“”
It is not enough to have a good mind; the main thing is to use it well.
RENE DESCARTES
Prepare();var mind = blown;
![Page 4: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/4.jpg)
This presentation is brought to you by Telerik.Don’t worry. This is hardcore ideas and code, not a sales pitch.
![Page 5: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/5.jpg)
Agenda
Background Information Data Structures
Design Patterns
LINQ
LINQ to Functions
LINQ to Graphs
LINQ to Specifications
![Page 6: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/6.jpg)
Data StructuresA PRIMER
![Page 7: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/7.jpg)
Common Data Structures
Primitive Types
Composite Types
Abstract Types
![Page 8: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/8.jpg)
Primitive Types
These are struct types in C#
Represents a single value
Examples: bool, char, float, int
![Page 9: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/9.jpg)
Composite Types
In C#, everything else is a form of a composite type
Typical examples Array
Record
Tuple
Struct (not the C# struct)
Plain Old Data Structure
Union
Tagged Union – Union that tracks current type
Object – Data + Behavior
![Page 10: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/10.jpg)
Abstract Types
These are abstract composite types, structural implementations vary
Examples Dictionary
List
Set
Tree
Graph
![Page 11: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/11.jpg)
Node
Graph
Composed of nodes (or vertices) and edges
1 2
3 4 5
Edge
![Page 12: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/12.jpg)
Digraph
Apply a direction
Removing direction is called the “underlying graph.”
1 2
3 4 5
![Page 13: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/13.jpg)
Weighted Graph
Edge contains value
Useful for determining optimal routes between destinations
1 2
3 4 5
2.2
1.5
6.03.7
0.5
![Page 14: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/14.jpg)
Multigraph
More than one edge allowed between nodes
1 2
3 4 5
![Page 15: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/15.jpg)
Hypergraph
Edges can connect more than two nodes
I view hyper-edges as categories
1 2
3 4 5
![Page 16: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/16.jpg)
Tree
A graph with parent-child relationships
No node can be the child of more than one node
1 2
3 4 5
6
![Page 17: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/17.jpg)
List
A graph where each node is connected to 1 or 2 nodes
Explicitly has an order
1 2 3 4 5
![Page 18: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/18.jpg)
Set Comparisons
List – Ordered, allows duplicates, single type
Tuple – Ordered, allows duplicates, multiple types
Set – Unordered, no duplicates, single type
![Page 19: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/19.jpg)
Design PatternsA PRIMER
![Page 20: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/20.jpg)
“”
A general reusable solution to a commonly occurring problem.
WIKIPEDIA
Even if you do not know design patterns, you’ve probably used them.
![Page 21: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/21.jpg)
Types of Design Patterns
Creational Patterns
Structural Patterns
Behavioral Patterns
Concurrency Patterns
Architectural Patterns
![Page 22: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/22.jpg)
Structural Patterns
Difference with Data Structures Focus is to use structure to enable behavior
There is potential overlap with abstract types
Examples Adapter
Composite
Decorator
Façade
Flyweight
![Page 23: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/23.jpg)
Behavioral Patterns
Difference with Structural Behavior isn’t necessarily driven by the structure
Examples Chain of Responsibility
Command
Iterator
Observer
Specification
Visitor
![Page 24: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/24.jpg)
Iterator
an object that provides a standard way to examine all element of any collection.
Has a uniform interface for traversing many data structure without exposing their implementations.
Supports concurrent iteration and element removal.
No need to know the internal structure of collection.
![Page 25: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/25.jpg)
Iterator
+ CreateIterator()
Aggregate<<interface>>
+ First()+ Next()+ IsDone()+ CurrentItem
Iterator<<interface>>
+ CreateIterator()
ConcreteAggregate ConcreteIterator
Client
![Page 26: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/26.jpg)
Observer
An object, called the subject, maintains a list of its dependents
Dependents are known as observers
Observers are notified when subject’s state changes
![Page 27: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/27.jpg)
Observer
+observers+Register(observer)+Unregister(observer)+Notify()
Subject
+Notify()
ConcreteObserverA ConcreteObserverB
+Notify()
Observer
![Page 28: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/28.jpg)
Visitor
Separates an algorithm from the structure on which it operates
![Page 29: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/29.jpg)
Visitor
+Accept(visitable)
Elementlike<<interface>>
+Visit(element)
Visitable<<interface>>
+Accept(visitable)
Element Visitor
Client
![Page 30: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/30.jpg)
Specification
Separates business rules from business objects
Represents object-oriented predicates and predicate combinators
Predicate – conditionals that can be passed
The pattern uses “IsSpecifiedBy” to indicate whether the predicate is successful
![Page 31: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/31.jpg)
Specification
+And()+IsSatisfiedBy()+Not()+Or()
ISpecification<<Interface>>
+left : ISpecification+right : ISpecification
AndSpecification
+left : ISpecification+right : ISpecification
OrSpecification
+And()+IsSatisfiedBy()+Not()+Or()
CompositeSpecification
+wrapped : ISpecification
NotSpecification
![Page 32: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/32.jpg)
LINQA PRIMER
![Page 33: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/33.jpg)
Language Integrated Query
Interrogate / manipulate data
Type safe
Misunderstood
![Page 34: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/34.jpg)
Example Query
from x in object1
from y in object2
select x + y; What can you tell me about
object1?
![Page 35: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/35.jpg)
LINQ to Objects
This is LINQ over the iterator pattern
The iterator pattern in C# is implemented with IEnumerable
Much more declarative than foreach loops
Lazy Execution
![Page 36: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/36.jpg)
LINQ to SQL/EF/Etc
Uses visitor pattern
Lambdas represent Expressions rather than functions
Visitor translates expressions into another language
![Page 37: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/37.jpg)
Reactive
LINQ to observer pattern
Items are pushed instead of pulled
Great for asynchronous programming
Is now open source
![Page 38: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/38.jpg)
Materialization
sequence.ToList();
This is typically wrong.
![Page 39: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/39.jpg)
Materialization
sequence.Materialize();
public static IEnumberable<T> Materialize( this IEnumerable<T> sequence){
return sequence.ToArray();}
![Page 40: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/40.jpg)
Memoization
Best of both worlds: Lazy Execution and Materialized results
Cache results as collection is iterated
![Page 41: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/41.jpg)
Code
![Page 42: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/42.jpg)
LINQ to FunctionsFUNCTIONAL COMBINATORS
![Page 43: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/43.jpg)
What Does LINQ to Functions Mean
Applying LINQ to new forms require reimagining the DSL
In the case of functions, LINQ combines functions
Each LINQ method is a functional combinator
![Page 44: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/44.jpg)
“”
a combinator is a function which builds program fragments from program fragments…
JOHN HUGHES
![Page 45: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/45.jpg)
Code
![Page 46: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/46.jpg)
LINQ to GraphsVERTICES AND EDGES
![Page 47: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/47.jpg)
Select
Selectv` + 11 2
3
2 3
4
![Page 48: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/48.jpg)
SelectMany
SelectManyv` + w`1 2
3,1 2
2 3
4
3 4
5
![Page 49: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/49.jpg)
SelectMany
2 3
4
3 4
5
Expand
![Page 50: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/50.jpg)
SelectMany
2 3
4 5Fold
![Page 51: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/51.jpg)
Where
Wherev` % 2 == 11 2
31 3
![Page 52: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/52.jpg)
Code
![Page 53: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/53.jpg)
LINQ to SpecificationsPREDICATE COMBINATORS
![Page 54: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/54.jpg)
Explanation
Predicates are functions, but they combine differently than other functions
Therefore, it makes more sense to wrap them so specific combinators can be applied
![Page 55: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/55.jpg)
LINQ Weirdness
from x in specification
where x != 0
select !x;
![Page 56: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/56.jpg)
Code
![Page 57: Esoteric LINQ and Structural Madness](https://reader033.vdocuments.site/reader033/viewer/2022061213/5498adf7b47959a14b8b45df/html5/thumbnails/57.jpg)
Q&A