C# - Raise the bar with functional & immutable constructs (Dutch)

29

Transcript of C# - Raise the bar with functional & immutable constructs (Dutch)

Page 1: C# - Raise the bar with functional & immutable constructs (Dutch)
Page 2: C# - Raise the bar with functional & immutable constructs (Dutch)

C#Raise the bar with Functional & Immutable constructs

Page 3: C# - Raise the bar with functional & immutable constructs (Dutch)

O, Kan dat ook?

Page 4: C# - Raise the bar with functional & immutable constructs (Dutch)

Today• Func is cool• Tuple is your friend• Immutable is a life saver• Practice makes perfect• Action!

Page 5: C# - Raise the bar with functional & immutable constructs (Dutch)

Actionpublic delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);

Page 6: C# - Raise the bar with functional & immutable constructs (Dutch)

Funcpublic delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);

Page 7: C# - Raise the bar with functional & immutable constructs (Dutch)

Tuplepublic class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable, Ituple

Page 8: C# - Raise the bar with functional & immutable constructs (Dutch)

Tuple: Equality

Page 9: C# - Raise the bar with functional & immutable constructs (Dutch)

Functions• Data in – Data out• Func in – Data out• Data in – Func out• Func in – Func out

Page 10: C# - Raise the bar with functional & immutable constructs (Dutch)

Functions: Data in – Data outx => 2 * x

Page 11: C# - Raise the bar with functional & immutable constructs (Dutch)

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

Page 12: C# - Raise the bar with functional & immutable constructs (Dutch)

Functions: Data in – Func outx => y => x + y

Page 13: C# - Raise the bar with functional & immutable constructs (Dutch)

Functions: Func in – Func out• Example: Partial application• Example: Memoize

Page 14: C# - Raise the bar with functional & immutable constructs (Dutch)

Partial Applicationvar f = (x, y) => x + y;var p = x => y => f(x, y);

Page 16: C# - Raise the bar with functional & immutable constructs (Dutch)

Memoization• Pure functions• Long running operations

Page 17: C# - Raise the bar with functional & immutable constructs (Dutch)

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.

Page 19: C# - Raise the bar with functional & immutable constructs (Dutch)

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

Page 22: C# - Raise the bar with functional & immutable constructs (Dutch)

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!

Page 23: C# - Raise the bar with functional & immutable constructs (Dutch)

Immutable?• Int• String• Tuple• IEnumerable• IReadOnlyCollection/List/Dictionary• IList

Page 24: C# - Raise the bar with functional & immutable constructs (Dutch)

Immutable Collections• ImmutableList<T>• ImmutableDictionary<TKey, TValue>• ImmutableSortedDictionary<TKey,

TValue>• ImmutableHashSet<T>• ImmutableSortedSet<T>• ImmutableStack<T>• ImmutableQueue<T>

Page 26: C# - Raise the bar with functional & immutable constructs (Dutch)

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)

Page 27: C# - Raise the bar with functional & immutable constructs (Dutch)

Immutable Coll.: Memory• Worse: Per Item• Better: No reserved space• Better: Immediate shrink after delete

Page 28: C# - Raise the bar with functional & immutable constructs (Dutch)

Why?• Less code• Reusable• More fun!

Page 29: C# - Raise the bar with functional & immutable constructs (Dutch)

Rick [email protected]/rickbeerendonk

Simple. Clear. Software.