Esoteric LINQAND STRUCTURAL MADNESS
I Think Therefore I Am    Chris Eargle    Mad Scientist    Telerik Evangelist    C# MVP    INETA Board of Directors ...
“   It is not enough to have a    good mind; the main    thing is to use it well.      RENE DESCARTES                     ...
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    L...
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...
Abstract Types    These are abstract composite types, structural implementations vary    Examples        Dictionary    ...
Graph    Composed of nodes (or vertices) and edges               Node                         1          2        Edge   ...
Digraph    Apply a direction    Removing direction is called the “underlying graph.”                           1        ...
Weighted Graph    Edge contains value    Useful for determining optimal routes between destinations                     ...
Multigraph    More than one edge allowed between nodes                        1         2                        3       ...
Hypergraph    Edges can connect more than two nodes    I view hyper-edges as categories                          1      ...
Tree    A graph with parent-child relationships    No node can be the child of more than one node                      1...
List      A graph where each node is connected to 1 or 2 nodes      Explicitly has an order                     1       ...
Set Comparisons    List – Ordered, allows duplicates, single type    Tuple – Ordered, allows duplicates, multiple types ...
Design PatternsA PRIMER
“   A general reusable    solution to a commonly    occurring problem.      WIKIPEDIA                                     ...
Types of Design Patterns    Creational Patterns    Structural Patterns    Behavioral Patterns    Concurrency Patterns ...
Structural Patterns    Difference with Data Structures        Focus is to use structure to enable behavior        There...
Behavioral Patterns    Difference with Structural        Behavior isn’t necessarily driven by the structure    Examples...
Iterator    an object that provides a standard way to examine all element of     any collection.    Has a uniform interf...
Iterator            Aggregate          Client        Iterator           <<interface>>                  <<interface>>      ...
Observer    An object, called the subject, maintains a list of its dependents    Dependents are known as observers    O...
Observer                                                          Subject                        Observer                 ...
Visitor     Separates an algorithm from the structure on which it operates
Visitor               Elementlike       Client        Visitable              <<interface>>                 <<interface>>  ...
Specification    Separates business rules from business objects    Represents object-oriented predicates and predicate c...
Specification    ISpecification               CompositeSpecification    <<Interface>> +And()                          +And...
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...
LINQ to Objects    This is LINQ over the iterator pattern    The iterator pattern in C# is implemented with IEnumerable ...
LINQ to SQL/EF/Etc    Uses visitor pattern    Lambdas represent Expressions rather than functions    Visitor translates...
Reactive    LINQ to observer pattern    Items are pushed instead of pulled    Great for asynchronous programming    Is...
Materialization sequence.ToList();                      This is typically                          wrong.
Materialization sequence.Materialize(); public static IEnumberable<T> Materialize(   this IEnumerable<T> sequence) {     r...
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, ...
“   a combinator is a function    which builds program    fragments from program    fragments…     JOHN HUGHES            ...
Code
LINQ to GraphsVERTICES AND EDGES
Select         1       2    v` + 1   2       3                     Select             3                     4
SelectMany            1       2      v` + w`   2       3   3       41   2   ,               SelectMany                3   ...
SelectMany       Expand   2       3   3       4                    4           5
SelectMany               2       3        Fold                   4       5
Where        1       2   v` % 2 == 1                    Where         1   3            3
Code
LINQ to SpecificationsPREDICATE COMBINATORS
Explanation    Predicates are functions, but they combine differently than other     functions    Therefore, it makes mo...
LINQ Weirdness from x in specification where x != 0 select !x;
Code
Q&A
Upcoming SlideShare
Loading in …5
×

Esoteric LINQ and Structural Madness

891 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
891
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Esoteric LINQ and Structural Madness

  1. 1. Esoteric LINQAND STRUCTURAL MADNESS
  2. 2. 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
  3. 3. “ It is not enough to have a good mind; the main thing is to use it well. RENE DESCARTES ”Prepare();var mind = blown;
  4. 4. This presentation is brought to you by Telerik.Don’t worry. This is hardcore ideas and code, not a sales pitch.
  5. 5. Agenda  Background Information  Data Structures  Design Patterns  LINQ  LINQ to Functions  LINQ to Graphs  LINQ to Specifications
  6. 6. Data StructuresA PRIMER
  7. 7. Common Data Structures  Primitive Types  Composite Types  Abstract Types
  8. 8. Primitive Types  These are struct types in C#  Represents a single value  Examples: bool, char, float, int
  9. 9. 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
  10. 10. Abstract Types  These are abstract composite types, structural implementations vary  Examples  Dictionary  List  Set  Tree  Graph
  11. 11. Graph  Composed of nodes (or vertices) and edges Node 1 2 Edge 3 4 5
  12. 12. Digraph  Apply a direction  Removing direction is called the “underlying graph.” 1 2 3 4 5
  13. 13. Weighted Graph  Edge contains value  Useful for determining optimal routes between destinations 2.2 1 2 3.7 6.0 1.5 0.5 3 4 5
  14. 14. Multigraph  More than one edge allowed between nodes 1 2 3 4 5
  15. 15. Hypergraph  Edges can connect more than two nodes  I view hyper-edges as categories 1 2 3 4 5
  16. 16. Tree  A graph with parent-child relationships  No node can be the child of more than one node 1 2 3 4 5 6
  17. 17. List  A graph where each node is connected to 1 or 2 nodes  Explicitly has an order 1 2 3 4 5
  18. 18. Set Comparisons  List – Ordered, allows duplicates, single type  Tuple – Ordered, allows duplicates, multiple types  Set – Unordered, no duplicates, single type
  19. 19. Design PatternsA PRIMER
  20. 20. “ A general reusable solution to a commonly occurring problem. WIKIPEDIA ”Even if you do not know design patterns, you’ve probably used them.
  21. 21. Types of Design Patterns  Creational Patterns  Structural Patterns  Behavioral Patterns  Concurrency Patterns  Architectural Patterns
  22. 22. 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
  23. 23. Behavioral Patterns  Difference with Structural  Behavior isn’t necessarily driven by the structure  Examples  Chain of Responsibility  Command  Iterator  Observer  Specification  Visitor
  24. 24. 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.
  25. 25. Iterator Aggregate Client Iterator <<interface>> <<interface>> + CreateIterator() + First() + Next() + IsDone() + CurrentItem ConcreteAggregate ConcreteIterator + CreateIterator()
  26. 26. 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
  27. 27. Observer Subject Observer +observers +Register(observer) +Notify() +Unregister(observer) +Notify() ConcreteObserverA ConcreteObserverB +Notify()
  28. 28. Visitor  Separates an algorithm from the structure on which it operates
  29. 29. Visitor Elementlike Client Visitable <<interface>> <<interface>> +Accept(visitable) +Visit(element) Element Visitor +Accept(visitable)
  30. 30. 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
  31. 31. Specification ISpecification CompositeSpecification <<Interface>> +And() +And() +IsSatisfiedBy() +IsSatisfiedBy() +Not() +Not() +Or() +Or() AndSpecification OrSpecification NotSpecification +left : ISpecification +left : ISpecification +right : ISpecification +right : ISpecification +wrapped : ISpecification
  32. 32. LINQA PRIMER
  33. 33. Language Integrated Query  Interrogate / manipulate data  Type safe  Misunderstood
  34. 34. Example Query from x in object1 from y in object2 select x + y; What can you tell me about object1?
  35. 35. 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
  36. 36. LINQ to SQL/EF/Etc  Uses visitor pattern  Lambdas represent Expressions rather than functions  Visitor translates expressions into another language
  37. 37. Reactive  LINQ to observer pattern  Items are pushed instead of pulled  Great for asynchronous programming  Is now open source
  38. 38. Materialization sequence.ToList(); This is typically wrong.
  39. 39. Materialization sequence.Materialize(); public static IEnumberable<T> Materialize( this IEnumerable<T> sequence) { return sequence.ToArray(); }
  40. 40. Memoization  Best of both worlds: Lazy Execution and Materialized results  Cache results as collection is iterated
  41. 41. Code
  42. 42. LINQ to FunctionsFUNCTIONAL COMBINATORS
  43. 43. 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
  44. 44. “ a combinator is a function which builds program fragments from program fragments… JOHN HUGHES ”
  45. 45. Code
  46. 46. LINQ to GraphsVERTICES AND EDGES
  47. 47. Select 1 2 v` + 1 2 3 Select 3 4
  48. 48. SelectMany 1 2 v` + w` 2 3 3 41 2 , SelectMany 3 4 5
  49. 49. SelectMany Expand 2 3 3 4 4 5
  50. 50. SelectMany 2 3 Fold 4 5
  51. 51. Where 1 2 v` % 2 == 1 Where 1 3 3
  52. 52. Code
  53. 53. LINQ to SpecificationsPREDICATE COMBINATORS
  54. 54. 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
  55. 55. LINQ Weirdness from x in specification where x != 0 select !x;
  56. 56. Code
  57. 57. Q&A

×