Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Lambda Expressions Are Cool
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Map() takes a function and applies it to each element in the array int[] data = new int[] {0,1,2,3,4,5}; Console.WriteLine(data.ArrayString()); data = data.Map(i => i * i); //note that if the only thing you're doing is returning a value, //you don't need a return expression or curly brackets. //if there is more than one parameter you need to surround them with parentheses. the types of these do not USUALLY need to be typed. Console.WriteLine(data.ArrayString()); bool[] bools = new bool[] {true,false,true,false}; Console.WriteLine(bools.ArrayString()); bools = bools.Map(b => !b); Console.WriteLine(bools.ArrayString()); //Another example of lambda, declaring a local function within a function Func<int,bool> positive = i => {Console.WriteLine(i); return i > 0;}; Console.WriteLine(positive(5)); Console.WriteLine(positive(-5)); //You could declare the same function the regular way like this: //public bool positive(int i) //{ // Console.WriteLine(i); // return i > 0; //} //(not inside of another function, though) //Void lambda, second Map() override string message = "Hello World!"; message.ToCharArray().Map(c => { Console.Write(((int)c).ToString() + ":"); Console.Write(c.ToString()); Console.Write(","); }); //...and you can put more than one parameter into a lambda expression, but because you need a delegate for each number of parameters, I'm just going to write a long comment about it. //if you have a delegate declared like "public delegate int intwoints(int a, int b);", then the lambda expression would be: //"intwoints example = (a,b) => a + b;" or something. //note that like passing parameters into a regular func you can name a and b whatever you want, just remeber to use those names in the body of your lambda. //I'm new to this tutorial thing so if there's something that's wrong or you didn't like here just tell me, thanks } } static class Extn { public delegate T2 Func<T1,T2>(T1 p); //replacing Func<> provided in VS public delegate void Action<T>(T p); //replacing Action<> provided in VS public static T[] Map<T>(this T[] a, Func<T,T> f) { T[] res = a; for(int x = 0; x < res.Length; x++) res[x] = f(res[x]); return res; } public static string ArrayString<T>(this T[] a) //prints out an array in "human-readable" format { string tor = "["; foreach(T t in a) tor += t.ToString() + ","; return tor.Remove(tor.Length - 1) + "]"; } public static void Map<T>(this T[] a, Action<T> f) { foreach(T t in a) f(t); } } }
run
|
edit
|
history
|
help
0
c#
sdcasdcasdcsadxcd
where index
my first
Dotnetq5
remove dups
Error
Linq Func Action
Square Real Matrix
Lab8