The document discusses mixing functional and object-oriented programming approaches in C#, covering topics like generics, LINQ, lambdas, anonymous methods, extension methods, and more. It provides examples of filtering arrays and enumerables using predicates in increasingly functional styles. It argues that functional programming can complement object-oriented code by abstracting over common operations.
22. Inference int[] ints = new int[] {1, 2, 3, 4, 5 }; int[] even = Filter(ints, new IntegerPredicate(Even)); Int[] greaterThanThree = Filter(ints, new IntegerPredicate(GreaterThanThree));
23. Inference int[] ints = new int[] {1, 2, 3, 4, 5 }; int[] even = Filter(ints, Even); Int[] greaterThanThree = Filter(ints, GreaterThanThree); The compiler can infer what the type of the delegate is so we don’t have to write it.
33. Anonymous Methods int minimumValue = 3; IEnumerable<int> greaterThanThree = Filter(ints, delegate(int value) { return value > minimumValue; }); Anonymous methods add support for closures. The delegate captures the scope it was defined in.
43. LINQ New delegates in System namespace Action<T>, Action<T1, T2>, Func<TResult>, Func<T1, TResult> etc.
44. LINQ New delegates in System namespace Action<T>, Action<T1, T2>, Func<TResult>, Func<T1, TResult> etc. System.Linq Extension methods Where, Select, OrderBy etc.
45. LINQ New delegates in System namespace Action<T>, Action<T1, T2>, Func<TResult>, Func<T1, TResult> etc. System.Linq Extension methods Where, Select, OrderBy etc. Some compiler magic to translate sql style code to method calls
46. LINQ var even = ints.Where(value => value % 2 == 0) var greaterThanThree = ints.Where(value => value > minimumValue) or var even = from value in ints where value % 2 == 0 select value var greaterThanThree = from value in ints where value > minimumValue select value
53. Transformational Mindset We can just pass functions around instead in most cases - find an example where it still makes sense to use the GOF approach though.
106. var people = new [] { new Person { Id=1, Address = new Address { Road = "Ewloe Road" }}, new Person { Id=2}, new Person { Id=3, Address = new Address { Road = "London Road"}} }; people.Select(p => p.Address.Road);
origins of functional programming are found in lambda calculation/maths
functions that take in a function or return a function. Need to have first class functions in the language to do that. We have that with all the LINQ methods - select, where, and so on.
the whole premise of functional programming with side effect free functions assumes that we have immutable data. We can't achieve this idiomatically in C# because the language isn't really designed for it. I want to put an example of how immutability is easy in F#, can that go in this section?
iterators in C# do this with yield keyword It's not necessary to have lazy evaluation to be functional but it's a characteristic of some functional languages.
seems quite obvious but the most extreme guideline to follow is that we shouldn't need to store anything in variables. Look at the data as a whole if we don't store any intermediate values then we truly do have some data that we are passing through different filters and applying some transformation
it's quite like the pipes and filters architectural pattern in fact. This is the way that we can combine functions on the unix command line.
what is CPS? is where we pass in a function that represents the rest of the program which will be called with the result of another function.
what is CPS? is where we pass in a function that represents the rest of the program which will be called with the result of another function.
what is CPS? is where we pass in a function that represents the rest of the program which will be called with the result of another function.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
Encapsulates the state but over complicates the program flow perhaps
Encapsulates the state but over complicates the program flow perhaps