0
Coding with LINQ,Patterns & Practices LINQ & Functional ways                          © Tuomas Hietanen (LGPLv3)
LINQ (Monads)• Two types of functions  – Queries and Aggreagtes                                                  AGGREGATE...
LINQ SELECT = LIST.MAP                (Query)                   MAP function:IEnumerable<   >                            I...
LINQ WHERE= LIST.FILTER               (Query)                   FILTER function:IEnumerable<   >                          ...
LINQ AGGREGATE= LIST.FOLD           (Aggregate)                   FOLD function:IEnumerable<   >                        © ...
Other Aggregates             (to make things easier)Function        Aggregate                functionLIST.SUM        Add e...
LINQ SELECTMANY- Not pure, has many overrides         SELECT function:                                   IEnumerable<   > ...
LINQ SELECTMANY- Not pure, has many overrides    SELECT function:                       © Tuomas Hietanen
Old Legacy Way               New C#, LINQ                     F#var result = new List<T>(); var result = from x in xs     ...
Do use lists!• Don’t return Null. Prefer throw  InvalidOperationException on error and  Enumerable.Empty<T>() on case of e...
Safe class design, avoid side effects• Don’t reuse variables     x=x+1     y=x+1• Avoid global and class variables. Use me...
F# list vs seq• List is F# default   – Linked list   – Good for recursive methods• Seq is IEnumerable<T>   – Lazy evaluati...
Upcoming SlideShare
Loading in...5
×

Coding with LINQ, Patterns & Practices

1,930

Published on

LINQ ja koodauskäytännöt (Finnish) http://www.youtube.com/watch?v=OFQltnLESOg

Coding with LINQ, Patterns & Practices (English) http://www.youtube.com/watch?v=YAha8tQyVHE

Slides: http://www.slideshare.net/thorium/coding-with-linq-patterns-practices

---

LINQ:lle refaktorointi (Finnish) http://www.youtube.com/watch?v=2wpqydogdPw

Refactoring to LINQ (English) http://www.youtube.com/watch?v=1gX0piE3xbk


F#-koodaus (Finnish) http://www.youtube.com/watch?v=WgVcJitKUZ8

Coding F# (English) http://www.youtube.com/watch?v=OA-zLw7xJm4

Source code: https://github.com/Thorium/Demo

Published in: Technology
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total Views
1,930
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
32
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Coding with LINQ, Patterns & Practices"

  1. 1. Coding with LINQ,Patterns & Practices LINQ & Functional ways © Tuomas Hietanen (LGPLv3)
  2. 2. LINQ (Monads)• Two types of functions – Queries and Aggreagtes AGGREGATE: - returns result (Wrapper) Wrapped type, In this case IEnumerable<T> QUERY: - returns another container of the same type © Tuomas Hietanen
  3. 3. LINQ SELECT = LIST.MAP (Query) MAP function:IEnumerable< > IEnumerable< > © Tuomas Hietanen
  4. 4. LINQ WHERE= LIST.FILTER (Query) FILTER function:IEnumerable< > IEnumerable< > © Tuomas Hietanen
  5. 5. LINQ AGGREGATE= LIST.FOLD (Aggregate) FOLD function:IEnumerable< > © Tuomas Hietanen
  6. 6. Other Aggregates (to make things easier)Function Aggregate functionLIST.SUM Add every toLIST.COUNT Add (+1) toLIST.MAX Select bigger of or... © Tuomas Hietanen
  7. 7. LINQ SELECTMANY- Not pure, has many overrides SELECT function: IEnumerable< > © Tuomas Hietanen
  8. 8. LINQ SELECTMANY- Not pure, has many overrides SELECT function: © Tuomas Hietanen
  9. 9. Old Legacy Way New C#, LINQ F#var result = new List<T>(); var result = from x in xs let result =foreach(var x in xs){ select … List.map (fun x -> ...) xs result.Add(...); //same as ... var result = xs.Select(x=>...);}foreach(var x in xs){ var result = from x in xs let result = if(x=...){...} where … List.filter(fun x -> ...) xs} //same as var result = xs.Where(x=>...);var result = new T(); //many ways, based on //many ways, based onforeach(var x in xs){ var result = xs.Aggregate(…); let result = result = ...; List.fold (fun f -> ...) xs ...} //also supports unfoldforeach(var x in xs){ var result = from x in xs //SelectMany is not pure, foreach(var y in ...){ from y in … so depends on situation. .... select … E.g. List.collect, or seq {...} } //same as let result =} var result = List.collect (fun x -> ...) xs xs.SelectMany(...); © Tuomas Hietanen
  10. 10. Do use lists!• Don’t return Null. Prefer throw InvalidOperationException on error and Enumerable.Empty<T>() on case of empty list• Don’t modify collections, do use new ones list.RemoveAt(0) newlist = list.Where(interestingItems => ...)• Use IEnumerable<T> not List<T> or IList<T> – It is the baseclass and default for LINQ – You can always say .toList() to evaluate the lazy collection. © Tuomas Hietanen
  11. 11. Safe class design, avoid side effects• Don’t reuse variables x=x+1 y=x+1• Avoid global and class variables. Use method parameters – Better for function composition• Don’t overcapuslate layers or class structures – No coding for fun: Focus on the functionality• Declarative programming. – ”yield return” is ok in some cases, but not really declarative.• Also old design practices do still apply (=> FxCop). © Tuomas Hietanen
  12. 12. F# list vs seq• List is F# default – Linked list – Good for recursive methods• Seq is IEnumerable<T> – Lazy evaluation – Good for .NET interop © Tuomas Hietanen
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×