static void Main(string[] args) { var buffer = new CircularBuffer<double>(capacity: 3); buffer.ItemDiscarded += buffer_ItemDiscarded ; ProcessInput(buffer); Action<double> consoleOut = delegate (double data) { Console.WriteLine(data); }; // OR // Action<double> consoleOut = d => Console.WriteLine(d); buffer.Dump(consoleOut); // OR // Without any Action's declaring et cetera, just use lambda expression: Converter<double, DateTime> converter = d => new DateTime(2010, 1, 1).AddDays(d); var asDates = buffer.Map(converter); foreach (var item in asDates) { Console.WriteLine(item); } buffer.Dump(d => Console.WriteLine(d)); ProcessBuffer(buffer); Console.WriteLine("Press enter to exit..."); Console.ReadLine(); }
static void Main(string[] args) { var standardBuffer = new Buffer <double>(); // Create new 'Standard' Buffer var circularBuffer = new CircularBuffer <double>(capacity: 3); // Create new Circular Buffer with a maximum capacity of: 3 circularBuffer.ItemDiscarded += Buffer_ItemDiscarded; // Subscribe to Event, capturing all 'discarded items' ProcessInput(circularBuffer); // Enter Buffer values Utilities.NewSectionHeading("Items - Not Converted", 2); Action <double> consoleOutput = Utilities.ConsoleWriteItem; // Pass Event Delegate, using built-in 'Action' type // var consoleOutput = new Printer<double>(Utilities.ConsoleWriteItem); circularBuffer.DumpBuffer(consoleOutput); // Loop through 'raw' Buffer and Print each item to Console Utilities.NewSectionHeading("Items - Converted To Integer", 2); var bufferConvertToInt = circularBuffer.AsEnumerableOf <double, int>(); // Use converter to, conert typeof(T) to an 'integer' foreach (var item in bufferConvertToInt) // Loop through Buffer and Print each converted item { Console.WriteLine($"Item: \t {item}"); // Print 'converted' Buffer to Console } Utilities.NewSectionHeading("Items - Converted To Date", 2); Converter <double, DateTime> converter = daysToAdd => new DateTime(2021, 1, 1).AddDays(daysToAdd); var bufferConvertToDates = circularBuffer.Map(converter); foreach (var item in bufferConvertToDates) { Console.WriteLine($"Item: \t {item}"); } ProcessBuffer(circularBuffer); // Calculate Buffer 'sum' and Print to Console Utilities.HaltProgramEnd(); // HALT PROGRAM END }
static void Main(string[] args) { var buffer = new CircularBuffer <double>(capacity: 3); //绑定事件委托 buffer.ItemDiscarded += ItemDiscarded; Converter <double, DateTime> converter = d => new DateTime(2019 / 08 / 07).AddDays(d); var output = buffer.Map(converter); foreach (var item in output) { Console.WriteLine($"item:{item}"); } ProcessInput(buffer); buffer.Dump(d => Console.WriteLine("dump:" + d)); ProcessBuffer(buffer); }
//static void ConsoleWrite(double data) //{ // Console.WriteLine(data); //} static void Main(string[] args) { //Action delegate //Action<double> print = delegate (double data){ // Console.WriteLine(data); //}; //lemda expresion virsui esancio kodo //delegates /*Action<bool> print = d => Console.WriteLine(d); * Func<double, double> square = d => d * d; * //paskutinis double(paskutinis narys) reiskia return type * Func<double, double, double> add = (x, y) => x + y; * * Predicate<double> isLessThanTen = d => d < 10; * //Predicate<tipas> paima tipa ir returnina visada bool * * print(isLessThanTen(add(square(1.2), square(1.1))));*/ var buffer = new CircularBuffer <double>(capacity: 3); buffer.ItemDiscarded += ItemDiscarded; ProcessInput(buffer); Converter <double, DateTime> converter = d => new DateTime(2018, 1, 1).AddDays(d); var asDate = buffer.Map <double, DateTime>(converter); foreach (var date in asDate) { Console.WriteLine(date); } Console.WriteLine("----------------------"); //var asInt = buffer.AsEnumemerableOf<double, int>(); ///var asInt = BufferExtensions.AsEnumemerableOf<double, int>(buffer); //foreach (var item in asInt) //{ //Console.WriteLine(item); // } Console.WriteLine("----------------------"); //buffer.Dump<double>(); //var consoleOut = new Printer<double>(ConsoleWrite); //virsutinio kodo netnereikia galima consolewrite is //kart i dup ideti kompiliatorius susigaudys //buffer.Dump<double>(); buffer.Dump(d => Console.WriteLine(d)); //Compiliatorius pats susigaudo koks tipas turibut ProcessBuffer(buffer); }
//same sginature of delegate Printer //static void ConsoleWrite(double data) //{ // Console.WriteLine(data); //} static void Main(string[] args) { //CTRL M THEN CTLR R to refractor extract, create a method //generics allow us to reuse code whilst still being type safe. So we create with a palceholder for a type and the calling client instanciates the type. //var buffer = new CircularBuffer(capacity: 3); //var buffer = new CircularBuffer<double>(capacity: 3); //But I can use action instead of my printer delegate //Action<double> print = ConsoleWrite; //using an anynomus delegate /*Action<double> print = delegate(double data) * { * Console.WriteLine(data); * };*/ //using lambda //Action is a void Action <Boolean> print = d => Console.WriteLine(d); //Func must return, last aurgument is always the return type //These are how linq works Func <double, double> square = d => d * d; Func <double, double, double> add = (x, y) => x + y; //Predicate aways returns a boolean Predicate <double> isLessThanTen = d => d < 10; //this then allows us to do things like this: print(isLessThanTen(square(add(3, 5)))); //var buffer = new Buffer<double>(); //lets use a circular buffer to demo events var buffer = new CircularBuffer <double>(capacity: 3); //creates a event handler for ItemDisgarded buffer.ItemDisgarded += ItemDisgarded; //hit tab tab for c# to create the template code //now we want to know when we hit capcity, best way is to reaise an event when we dequeue //using the convert to ints //var asInts = buffer.AsEnumerableOf<double, int>(); //var consoleOut = new Printer<double>(ConsoleWrite); //actuall dont need this //buffer.Dump(); //buffer.Dump<double>(); dont have to do this as it already knows buffer is a double //buffer.Dump(consoleOut); //now with printer delegate //buffer.Dump(ConsoleWrite); //without variable definition //using the action ConsoleWrite buffer.Dump(d => Console.WriteLine(d)); ProcessInput(buffer); //this won't work as AsEnumerableOf uses GetConverter which can't do a double to an dattime so throws an error //var asDates = buffer.AsEnumerableOf<double, DateTime>(); //lets look at built in coverter delegate //Converter<double, DateTime> converter = d => new DateTime(2010,1,1).AddDays(d); //can just pass in the lamda like this var asDates = buffer.Map(d => new DateTime(2010, 1, 1).AddDays(d)); //using map name foreach (var date in asDates) { Console.WriteLine(date); } //foreach (var item in buffer) //I need a enumerable inferace to do this - done /* * foreach (var item in asInts) //loop through ints * { * Console.WriteLine(item); * } */ ProcessOutput(buffer); }