Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Lambda Expressions Are Cool 2
//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 { delegate string Del(int pr); static string ExampleFunc(int prm) { return (prm + 1).ToString(); } public static void Main(string[] args) { //...After learning some more C#, I'm back with my second version of "Lambda Expressions Are Cool". //This tutorial assumes you know C# generics. //Before we talk about Lambda Functions, we have to talk about delegates. //A delegate is a blueprint for a function that can be used as a type. //If you look above the Main method, you can see I declared a delegate function Del. //It's the blueprint for a function that takes in a single int parameter, //and returns a string. The signature, as it's called, would look like this: //(int)->string. This means a "Del" function is any function that also has //the signature (int)->string. So: Del exampleDel1 = ExampleFunc; //This works because ExampleFunc's signature matches Del's signature. Console.WriteLine(exampleDel1.Invoke(10)); //exampleDel1 can now be treated like a variable, Console.WriteLine(exampleDel1(10)); //but can be called like a function with .Invoke() or just () //Microsoft provides generic delegates that will be used from now on: Func<> and Action<>. //In Func<>, the last type will always be the return type, and any other types will be parameters. //Action<> is the same, except the void return type. Func<int,string> exampleDel2 = ExampleFunc; //Func<int,string> = (int)->string, the same as Del. Console.WriteLine("Still {0}", exampleDel2(10)); //Now, we could always declare a function regularly, but you'd need to declare a function //for every time you need to pass it in a parameter. However, there's another way to declare //a function that's inline, and it's the main basis for C#'s function programming: the Lambda Function. Func<int,string> exampleDel3 = prm => (prm + 1).ToString(); //What does this mean!? Well, notice that it's using the same names and expression //as ExampleFunc. In fact, it's exactly the same in functionality as ExampleFunc. //exampleDels 1,2, and 3 are all the same. Let's break down the Lambda expression: //prm =>: => is the symbol to declare a lambda function, and prm is the name of the function parameter //(prm + 1).ToString(): The body of the function. If all you're doing in a lambda function is //a return expression, you don't need to put "return". Here's an example of a multi-line Lambda: Func<int,int,int> addAndSquare = (x,y) => { //(int,int)->int int z = x + y; return z * z; }; //This can be any function body. Console.WriteLine(addAndSquare(5,7)); //So now that we know how to use Lambda Functions, let's use it practically: to map a function onto an array. int[] arrints = {0,1,2,3,4,5}; string[] arrintstrs = arrints.Map(i => i.ToString() + "i"); //(int)->string Console.WriteLine(arrints.AsStr()); Console.WriteLine(arrintstrs.AsStr()); //We can also take the sum of arrints by using fold: int sum = arrints.Fold(0, (i,o) => i + o); //Just one line!! (int,int)->int //In fold, we take an initial value, then for each element in the array, //set the initial value to some function taking the array element (i) and the old value of the initial (o). Console.WriteLine("Sum: {0}", sum); //Like map, I can make Fold return any type: Console.WriteLine(arrints.Fold("", (i,o) => o + i)); //(int,string)->string //So, in summary, what is a Lambda Function? It's a way to declare an anonymous, inline //function that is very useful for quickly and efficiently passing in a delegate parameter. //Functions like Map and Fold are extremely useful in program clearness. Functional programming //is the next big innovation after Object Orientation, and Lambda Functions are C#'s implementation of it. } } static class Extn //Extension methods must be in a static class, so this is the class { public static Out[] Map<In,Out>(this In[] a, Func<In,Out> f) { //Takes in an array and applies a function to each of its elements. Out[] tor = new Out[a.Length]; for(int x = 0; x < a.Length; x++) tor[x] = f(a[x]); return tor; } public static IEnumerable<Out> Map<In,Out>(this IEnumerable<In> a, Func<In,Out> f) { //Like map with array, but works with any IEnumerable. foreach(In i in a) yield return f(i); } public static Out Fold<In,Out>(this IEnumerable<In> a, Out initial, Func<In,Out,Out> f) { //"Folds" an IEnumerable into a single value Out o = initial; foreach(In i in a) o = f(i,o); return o; } public static string AsStr<T>(this IEnumerable<T> a) { //Represents the enumerable as a human-readable string string res = a.Fold("[", (i,o) => o + i + ", "); return res.Remove(res.Length-2) + "]"; } } }
run
|
edit
|
history
|
help
3
Консоль. Депозит
Generic class and method example in C#
Exception Handling
changeMe
Can a == true && a == false be true in C#?
Compiler
Proj2
DN2
PolyMorphism
30272 Program Ex7_2 switch