Esoteric LINQ and Structural Madness

57
Esoteric LINQ AND STRUCTURAL MADNESS

description

 

Transcript of Esoteric LINQ and Structural Madness

Page 1: Esoteric LINQ and Structural Madness

Esoteric LINQAND STRUCTURAL MADNESS

Page 2: Esoteric LINQ and Structural Madness

I Think Therefore I Am

Chris Eargle

Mad Scientist

Telerik Evangelist

C# MVP

INETA Board of Directors

Contacts kodefuguru.com

[email protected]

callto://kodefuguru

Page 3: Esoteric LINQ and Structural Madness

“”

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

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

Agenda

Background Information Data Structures

Design Patterns

LINQ

LINQ to Functions

LINQ to Graphs

LINQ to Specifications

Page 6: Esoteric LINQ and Structural Madness

Data StructuresA PRIMER

Page 7: Esoteric LINQ and Structural Madness

Common Data Structures

Primitive Types

Composite Types

Abstract Types

Page 8: Esoteric LINQ and Structural Madness

Primitive Types

These are struct types in C#

Represents a single value

Examples: bool, char, float, int

Page 9: Esoteric LINQ and Structural Madness

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

Abstract Types

These are abstract composite types, structural implementations vary

Examples Dictionary

List

Set

Tree

Graph

Page 11: Esoteric LINQ and Structural Madness

Node

Graph

Composed of nodes (or vertices) and edges

1 2

3 4 5

Edge

Page 12: Esoteric LINQ and Structural Madness

Digraph

Apply a direction

Removing direction is called the “underlying graph.”

1 2

3 4 5

Page 13: Esoteric LINQ and Structural Madness

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

Multigraph

More than one edge allowed between nodes

1 2

3 4 5

Page 15: Esoteric LINQ and Structural Madness

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

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

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

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

Design PatternsA PRIMER

Page 20: Esoteric LINQ and Structural Madness

“”

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

Types of Design Patterns

Creational Patterns

Structural Patterns

Behavioral Patterns

Concurrency Patterns

Architectural Patterns

Page 22: Esoteric LINQ and Structural Madness

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

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

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

Iterator

+ CreateIterator()

Aggregate<<interface>>

+ First()+ Next()+ IsDone()+ CurrentItem

Iterator<<interface>>

+ CreateIterator()

ConcreteAggregate ConcreteIterator

Client

Page 26: Esoteric LINQ and Structural Madness

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

Observer

+observers+Register(observer)+Unregister(observer)+Notify()

Subject

+Notify()

ConcreteObserverA ConcreteObserverB

+Notify()

Observer

Page 28: Esoteric LINQ and Structural Madness

Visitor

Separates an algorithm from the structure on which it operates

Page 29: Esoteric LINQ and Structural Madness

Visitor

+Accept(visitable)

Elementlike<<interface>>

+Visit(element)

Visitable<<interface>>

+Accept(visitable)

Element Visitor

Client

Page 30: Esoteric LINQ and Structural Madness

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

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

LINQA PRIMER

Page 33: Esoteric LINQ and Structural Madness

Language Integrated Query

Interrogate / manipulate data

Type safe

Misunderstood

Page 34: Esoteric LINQ and Structural Madness

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

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

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

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

Materialization

sequence.ToList();

This is typically wrong.

Page 39: Esoteric LINQ and Structural Madness

Materialization

sequence.Materialize();

public static IEnumberable<T> Materialize( this IEnumerable<T> sequence){

return sequence.ToArray();}

Page 40: Esoteric LINQ and Structural Madness

Memoization

Best of both worlds: Lazy Execution and Materialized results

Cache results as collection is iterated

Page 41: Esoteric LINQ and Structural Madness

Code

Page 42: Esoteric LINQ and Structural Madness

LINQ to FunctionsFUNCTIONAL COMBINATORS

Page 43: Esoteric LINQ and Structural Madness

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

“”

a combinator is a function which builds program fragments from program fragments…

JOHN HUGHES

Page 45: Esoteric LINQ and Structural Madness

Code

Page 46: Esoteric LINQ and Structural Madness

LINQ to GraphsVERTICES AND EDGES

Page 47: Esoteric LINQ and Structural Madness

Select

Selectv` + 11 2

3

2 3

4

Page 48: Esoteric LINQ and Structural Madness

SelectMany

SelectManyv` + w`1 2

3,1 2

2 3

4

3 4

5

Page 49: Esoteric LINQ and Structural Madness

SelectMany

2 3

4

3 4

5

Expand

Page 50: Esoteric LINQ and Structural Madness

SelectMany

2 3

4 5Fold

Page 51: Esoteric LINQ and Structural Madness

Where

Wherev` % 2 == 11 2

31 3

Page 52: Esoteric LINQ and Structural Madness

Code

Page 53: Esoteric LINQ and Structural Madness

LINQ to SpecificationsPREDICATE COMBINATORS

Page 54: Esoteric LINQ and Structural Madness

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

LINQ Weirdness

from x in specification

where x != 0

select !x;

Page 56: Esoteric LINQ and Structural Madness

Code

Page 57: Esoteric LINQ and Structural Madness

Q&A