Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Sunday, 16 August 2015
( C#6 | Functional )

http://mikehadlow.blogspot.co.uk/2015/08/c-program-entirely-with-static-methods.html

and my fork: https://github.com/djhmateer/FunctionalDemo

"More concise and easier to test"

"Gone are interfaces and instance classes to be replaced by static methods, higher-order functions and closures"

Procedural –> Object-Oriented –> Functional

OO code .. "is typical of much well-constructed C# code that you will find in the wild"

OO Example

Why is composition root a static class?
Put properties below the ctor?
Why class properties and not a class field?
Why Console.Out.Writeline and not Console.WriteLine.. no real difference
RunCustomerReportBatch – interesting how to test this method, when it returns void, so have to test it’s dependencies’ results (the mocks)

public Report CreateCustomerReport(Customer customer) { // C#6 string interpolation var body = $"This is the report for {customer.Email}!"; return new Report(customer.Email, body); }

So this is exciting – new C#6 features.  Stable release of C#6 was on 20th July 2015 as part of .NET4.6. 

image
However I’m targeting 4.5.2, and not 4.6 (which is installed).  In fact it works targeting 4.5

C#6 requires the new Roslyn compiler http://stackoverflow.com/a/28921749/26086  but the compiler can target older versions of the framework for some features eg string interpolation as it results in a call to String.Format.

image

Functional Example

private static void Main() { // Action is a delegate. Doesn't return a value Action runProcessing = () => RunProcessing(Log); runProcessing(); } public static void RunProcessing(Action<string> log) { log("test"); } public static void Log(string message) { Console.WriteLine("log: {0}", message); }

Composing, then running a method which has a dependency.

 

Notes:

http://www.programgood.net/2014/10/24/70483FunQuestions.aspx going through Action and Func

http://www.pluralsight.com/courses/csharp-fundamentals-2
Functional bit

| | #