Coding with LINQ, Patterns & Practices
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Coding with LINQ, Patterns & Practices

  • 2,342 views
Uploaded on

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

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • LINQ is not actually just about collections. LINQ is context-unaware programming. You don't have to care about the inner state of a context.
    Are you sure you want to
    Your message goes here
    Be the first to like this
No Downloads

Views

Total Views
2,342
On Slideshare
2,319
From Embeds
23
Number of Embeds
1

Actions

Shares
Downloads
29
Comments
1
Likes
0

Embeds 23

http://killadonut.wordpress.com 23

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Coding with LINQ,Patterns & Practices LINQ & Functional ways © Tuomas Hietanen (LGPLv3)
  • 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. LINQ SELECT = LIST.MAP (Query) MAP function:IEnumerable< > IEnumerable< > © Tuomas Hietanen
  • 4. LINQ WHERE= LIST.FILTER (Query) FILTER function:IEnumerable< > IEnumerable< > © Tuomas Hietanen
  • 5. LINQ AGGREGATE= LIST.FOLD (Aggregate) FOLD function:IEnumerable< > © Tuomas Hietanen
  • 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. LINQ SELECTMANY- Not pure, has many overrides SELECT function: IEnumerable< > © Tuomas Hietanen
  • 8. LINQ SELECTMANY- Not pure, has many overrides SELECT function: © Tuomas Hietanen
  • 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. 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. 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. 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