Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Monday, 07 September 2015
( Action | Func | Functional )

Source is: https://github.com/djhmateer/Refactoring

The program does a:

  • Extract from a csv file of quotes
  • Transform using a ParseLine to split each line into a title and a quote
  • Load to insert into a database

Its a Console application designed to be run every night.  So just simple enough to really be able to explore techniques.  Based off http://mikehadlow.blogspot.co.uk/2015/08/c-program-entirely-with-static-methods.html

First Iteration which is using the blog post above’s architecture

// Functional style of the QuoteImporter app private static void Main() { // Compose the Functions together (without running them) Action run = () => QuoteImporter(ReadFileListOfLines, ParseLine, InsertQuoteIntoDatabase); run(); } // 1. Takes a Function with no input, which returns a list of strings // 2. Takes a Function with a string input, which returns a Quote // 3. Takes an Action with a Quote input public static void QuoteImporter( Func<IEnumerable<string>> readFileListOfLines, Func<string, Quote> parseLine, Action<Quote> insertQuoteIntoDatabase) { IEnumerable<string> lines = readFileListOfLines(); foreach (var line in lines) { var quote = parseLine(line); insertQuoteIntoDatabase(quote); } } // If there is a return type, then must be a Func<input, input..., output> public static IEnumerable<string> ReadFileListOfLines() { return File.ReadAllLines(@"..\..\quotesWithTitles.csv"); } public static Quote ParseLine(string line) { string[] values = line.Split(','); if (values.Length < 2) throw new ApplicationException("Unknown state - too few commas"); if (values.Length > 2) throw new ApplicationException("Unknown state - too many commas"); string title = values[0]; string body = values[1]; return new Quote { Title = title, Body = body }; } private static void InsertQuoteIntoDatabase(Quote quote) { string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Dev\Refactoring\Refactoring\Database1.mdf;Integrated Security=True"; using (var connection = new SqlConnection(connectionString)) { var cmd = new SqlCommand("INSERT INTO Quotes (Title, Text) VALUES (@Title, @Quote)"); cmd.Connection = connection; cmd.Parameters.AddWithValue("@Title", quote.Title); cmd.Parameters.AddWithValue("@Quote", quote.Body); connection.Open(); cmd.ExecuteNonQuery(); } }
| | # 
# Friday, 24 October 2014
( Action | c# | Func )
var a = Days.Mon; var x = Days.Wed; var b = (int)a; var c = (int) x; } } enum Days { Mon = 5, Tue, Wed, Thur, Fri, Sat, Sun };

Interesting: sequence will be 5,6,7…

From http://www.wrox.com/WileyCDA/WroxTitle/productCd-1118612094,descCd-DOWNLOAD.html

// Is k < 10 // cw // k++ for (int k = 0; k < 10; k++){ Console.WriteLine("hello {0}", k); }

Order of execution.

var curr = "£1,234.56"; double number; double.TryParse(curr, NumberStyles.Currency,null, out number);

Interesting!

var sss = new string('c', 10);

Makes 10 c’s

checked { int test = int.MaxValue; test++; //System.OverflowException thrown }


Checking for arithmetic overflow/underflow errors

float zero = 0; float positive = 1 / zero; Console.WriteLine(positive); // Outputs "Infinity" float negative = -1 / zero; Console.WriteLine(negative); // Outputs "-Infinity" int test = int.MaxValue; test++; Console.WriteLine(test); // int.MinValue var x = int.MinValue; checked { int xy = int.MaxValue; //xy++; // Overflow exception } checked{ float xx = float.MaxValue; var xxx = xx + 1; // no exception if (xx == xxx) // loss of equality in rounding Console.WriteLine("true"); // gets here }


number stuff.

class Program { static void Main(string[] args) { var t = new Thing("hello"); } } class ThingBase { public ThingBase(string message) { Console.WriteLine("ctor of thingbase {0}", message); } } class Thing : ThingBase { public string Message { get; set; } public Thing(string message) : base(message) { Console.WriteLine("ctor of customer"); this.Message = message; } }


Calling a base constructor

Implicity vs Explicity Interface impelement http://stackoverflow.com/questions/4103300/why-implement-interface-explicitly

class Program { static void Main(string[] args) { // TeachingAssistant can't see PrintGrades - public not possible IStudent ta = new TeachingAssistant(); ta.PrintGrades(); } } class TeachingAssistant : IStudent { void IStudent.PrintGrades() { throw new NotImplementedException(); } } interface IStudent { void PrintGrades(); }

Interesting explicit interface implementation.

Action and Func

// Action takes parameters but doesn't return Action note = () => Console.WriteLine("hello"); note(); Action<string> thing = x => Console.WriteLine("thing is {0}", x); thing("ahhh"); // Func is a generic type which encapsulates delegates (callable code) Func<float, float> square; square = delegate(float x) { return x*x; }; square = (float x) => x*x ; square = x => x*x ; var result = square(3); Console.WriteLine(result);
| | #