Esoteric LINQ and Structural Madness

Post on 23-Dec-2014

533 views 3 download

Tags:

description

 

transcript

Esoteric LINQAND STRUCTURAL MADNESS

I Think Therefore I Am

Chris Eargle

Mad Scientist

Telerik Evangelist

C# MVP

INETA Board of Directors

Contacts kodefuguru.com

kodefuguru@live.com

callto://kodefuguru

“”

It is not enough to have a good mind; the main thing is to use it well.

RENE DESCARTES

Prepare();var mind = blown;

This presentation is brought to you by Telerik.Don’t worry. This is hardcore ideas and code, not a sales pitch.

Agenda

Background Information Data Structures

Design Patterns

LINQ

LINQ to Functions

LINQ to Graphs

LINQ to Specifications

Data StructuresA PRIMER

Common Data Structures

Primitive Types

Composite Types

Abstract Types

Primitive Types

These are struct types in C#

Represents a single value

Examples: bool, char, float, int

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

Abstract Types

These are abstract composite types, structural implementations vary

Examples Dictionary

List

Set

Tree

Graph

Node

Graph

Composed of nodes (or vertices) and edges

1 2

3 4 5

Edge

Digraph

Apply a direction

Removing direction is called the “underlying graph.”

1 2

3 4 5

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

Multigraph

More than one edge allowed between nodes

1 2

3 4 5

Hypergraph

Edges can connect more than two nodes

I view hyper-edges as categories

1 2

3 4 5

Tree

A graph with parent-child relationships

No node can be the child of more than one node

1 2

3 4 5

6

List

A graph where each node is connected to 1 or 2 nodes

Explicitly has an order

1 2 3 4 5

Set Comparisons

List – Ordered, allows duplicates, single type

Tuple – Ordered, allows duplicates, multiple types

Set – Unordered, no duplicates, single type

Design PatternsA PRIMER

“”

A general reusable solution to a commonly occurring problem.

WIKIPEDIA

Even if you do not know design patterns, you’ve probably used them.

Types of Design Patterns

Creational Patterns

Structural Patterns

Behavioral Patterns

Concurrency Patterns

Architectural Patterns

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

Behavioral Patterns

Difference with Structural Behavior isn’t necessarily driven by the structure

Examples Chain of Responsibility

Command

Iterator

Observer

Specification

Visitor

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.

Iterator

+ CreateIterator()

Aggregate<<interface>>

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

Iterator<<interface>>

+ CreateIterator()

ConcreteAggregate ConcreteIterator

Client

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

Observer

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

Subject

+Notify()

ConcreteObserverA ConcreteObserverB

+Notify()

Observer

Visitor

Separates an algorithm from the structure on which it operates

Visitor

+Accept(visitable)

Elementlike<<interface>>

+Visit(element)

Visitable<<interface>>

+Accept(visitable)

Element Visitor

Client

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

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

LINQA PRIMER

Language Integrated Query

Interrogate / manipulate data

Type safe

Misunderstood

Example Query

from x in object1

from y in object2

select x + y; What can you tell me about

object1?

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

LINQ to SQL/EF/Etc

Uses visitor pattern

Lambdas represent Expressions rather than functions

Visitor translates expressions into another language

Reactive

LINQ to observer pattern

Items are pushed instead of pulled

Great for asynchronous programming

Is now open source

Materialization

sequence.ToList();

This is typically wrong.

Materialization

sequence.Materialize();

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

return sequence.ToArray();}

Memoization

Best of both worlds: Lazy Execution and Materialized results

Cache results as collection is iterated

Code

LINQ to FunctionsFUNCTIONAL COMBINATORS

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

“”

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

JOHN HUGHES

Code

LINQ to GraphsVERTICES AND EDGES

Select

Selectv` + 11 2

3

2 3

4

SelectMany

SelectManyv` + w`1 2

3,1 2

2 3

4

3 4

5

SelectMany

2 3

4

3 4

5

Expand

SelectMany

2 3

4 5Fold

Where

Wherev` % 2 == 11 2

31 3

Code

LINQ to SpecificationsPREDICATE COMBINATORS

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

LINQ Weirdness

from x in specification

where x != 0

select !x;

Code

Q&A