//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); }
static void Main(string[] args) { //Action<bool> print = d => Console.WriteLine(d); //Func<double, double> square = d => d * d; //Func<double, double, double> add = (x, y) => x + y; //Predicate<double> isLessThanTen = d => d < 10; //print(isLessThanTen(square(add(3,5)))); //var consoleOut = new Printer<double>(ConsoleWrite); //Action<double> print = delegate (double data) //{ // Console.WriteLine(data); //}; //Action<double> print = d => Console.WriteLine(d); //var buffer = new Buffer<double>(); //ProcessInput(buffer); //buffer.Dump(d => Console.WriteLine(d)); //Console.WriteLine("-----------As as Double-------------"); //foreach (var item in buffer) //{ // Console.WriteLine(item); //} //Console.WriteLine("-------------As date-------------"); //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); //} //Console.WriteLine("-------------As date with only labda expression-------------"); //var adDatewithLambda = buffer.Map(d => new DateTime(2010, 1, 1).AddDays(d)); //foreach (var item in adDatewithLambda) //{ // Console.WriteLine(item); //} //ProcessBuffer(buffer); var buffer = new CircularBuffer <double>(capacity: 3); buffer.ItemDiscarded += ItemDiscarded; ProcessInput(buffer); buffer.Dump(d => Console.WriteLine(d)); 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); }