c# - raise the bar with functional & immutable constructs (dutch)
TRANSCRIPT
C#Raise the bar with Functional & Immutable constructs
O, Kan dat ook?
Today• Func is cool• Tuple is your friend• Immutable is a life saver• Practice makes perfect• Action!
Actionpublic delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);
Funcpublic delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
Tuplepublic class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable, Ituple
Tuple: Equality
Functions• Data in – Data out• Func in – Data out• Data in – Func out• Func in – Func out
Functions: Data in – Data outx => 2 * x
Functions: Func in – Data out• LINQ: Where + more• Example: Object initialize• GitHub: https://
github.com/NForza/Functional/tree/master/Samples/ObjectInitialization
• Example: Aggregate• GitHub:
https://github.com/NForza/Functional/tree/master/Samples/Aggregate
Functions: Data in – Func outx => y => x + y
Functions: Func in – Func out• Example: Partial application• Example: Memoize
Partial Applicationvar f = (x, y) => x + y;var p = x => y => f(x, y);
Partial Application• GitHub: https://
github.com/NForza/Functional/tree/master/Samples/Partial
Memoization• Pure functions• Long running operations
Memoization CacheNow:• Unlimited size, unlimited time
Ideas:• Limited size, unlimited time• Unlimited size, limited time• Don’t use the cache unless real
function takes > 1 sec.
Memoization• GitHub: https://
github.com/NForza/Memoization• NuGet: https://
www.nuget.org/packages/Memoization
Abstractions• 1 or n• for
• Is Index needed?• Is List/IList the correct abstraction (do we
need to change the list?)
• foreach• What & How are together• I hear functional abstraction
• Return: IEnumerable, IDictionary, IGroup
Abstraction Examples• Interactive Extensions
• NuGet:https://www.nuget.org/packages/Ix-Main/
• NForza Extensions• GitHub:
https://github.com/NForza/Functional• NuGet: Soon!
• Weighted Average• GitHub: https://
github.com/NForza/Functional/blob/master/Sources/NForza.Functional/EnumerableExtensions.cs#L563-601
Miscellaneous Examples• Defer (make Lazy)• GitHub:
https://github.com/NForza/Functional/tree/master/Samples/Defer
• Bindings• GitHub: https://
github.com/rickbeerendonk/Binding• NuGet: https://
www.nuget.org/packages/Beerendonk.Binding
Immutable: Why?• Easier reasoning• Equality Has Meaning
(not in .NET’s immutable colleactions)• Sharing = Easy, no defensive copying
required!No ToArray() / ToList()• Concurrent Programming: No locks!
Immutable?• Int• String• Tuple• IEnumerable• IReadOnlyCollection/List/Dictionary• IList
Immutable Collections• ImmutableList<T>• ImmutableDictionary<TKey, TValue>• ImmutableSortedDictionary<TKey,
TValue>• ImmutableHashSet<T>• ImmutableSortedSet<T>• ImmutableStack<T>• ImmutableQueue<T>
Immutable Collections• Demo: https://
github.com/rickbeerendonk/ImmutableDemo• NuGet:
https://msdn.microsoft.com/en-us/library/dn769092(v=vs.110).aspx
Immutable Coll.: Performance
Mutable (amortized)
Mutable(worst case)
Immutable
Stack.Push O(1) O(n) O(1)
Queue.Enqueue O(1) O(n) O(1)
List.Add O(1) O(n) O(log n)
HashSet.Add O(1) O(n) O(log n)
SortedSet.Add O(log n) O(n) O(log n)
Dictionary.Add O(1) O(n) O(log n)
SortedDictionary.Add O(log n) O(n log n) O(log n)
Immutable Coll.: Memory• Worse: Per Item• Better: No reserved space• Better: Immediate shrink after delete
Why?• Less code• Reusable• More fun!
Rick [email protected]/rickbeerendonk
Simple. Clear. Software.