I just can't seem to ponder Lambdas in C #

I really could not get a clear idea about creating and using lambda expressions. I know how to use them in linq expressions, but I really don't understand what is going on behind the scenes. I also could not find a complete tutorial on when to use them, how to identify them, etc.

The second part of...

They say that Javascript is the LAMBDA language, I know javascript pretty well, I just wonder what types of concepts apply to javascript lambdas and C # lambdas.

thrid part ...

What is the difference between a functional language and a lambda language?

Any suggestions?

+3
source share
9 answers

, . , .

, :

delegate int D(int x);
...
class C
{
    void M(int y)
    {
        int z = 123;
        D d = x=>x+y+z;
        Console.WriteLine(d(10));
        z = 345;
        y = 789;
        Console.WriteLine(d(10));
   }
}

, :

delegate int D(int x);
...
class C
{  
    private class Locals
    {
        public int y;
        public int z;
        public int A(int x)
        {
            return x + this.y + this.z;
        }
    }
    void M(int y)
    {
        // Initialize the closure class:

        Locals locals = new Locals();
        locals.y = y;

        // Transform the body so that all usages of y, z and the lambda
        // refer to the closure class:

        locals.z = 123;
        D d = locals.A;
        Console.WriteLine(d(10)); // Calls locals.A(10)
        locals.z = 345;
        locals.y = 789;
        Console.WriteLine(d(10)); // Calls locals.A(10)

    }
}

. - " , , ".

JScript , , . JScript, , , , . # locals, . JScript , , .

lambdas , .

+8

, 1 2 , , 3.

? , . VS2010 .

, - , . , ( , ). .

" ", " ", , , , LINQ . AFAIK , Javascript - -, , .

, " ", . , :

", ,

, row = > row.State == "NY" " true, State State -".

, , .

+5

JavaScript. JS (var fn = function(){/* stuff */};), . , lambdas, - . :

// Standard sort:
x = [4,3,6,7,1,5,2];
x.sort();

// Custom sort:
y = [
    {'val':4,'name':'four'},
    {'val':3,'name':'three'},
    {'val':6,'name':'six'},
    {'val':7,'name':'seven'},
    {'val':1,'name':'one'},
    {'val':5,'name':'five'},
    {'val':2,'name':'two'},
];
y.sort(function(a,b){ return a.val > b.val ? 1 : -1 });

replace() - , - .

, , - ( - , , ).

. , Widget, . , , , . , . :

, Widget. , Widget.prototype.publish() , :

var Widget = function() {
    var self = this;
    var strPrivateVar = "This is a private variable";
    self.publicVar = "This is a default public variable";
    self.publish = function(f) {
        var fnFormatter = f;
        var strOutput = "The output is " + fnFormatter(self,strPrivateVar);
        return strOutput;
    }
};

, formatters. , :

var fnSummary = function(o,s) {
    var self = o;
    var strPrivateVar = s;
    return strPrivateVar.substr(0,5) + ' ' + self.publicVar.substr(0,5);
}
var fnDetails = function(o,s) {
    var self = o; 
    var strPrivateVar = s;
    return strPrivateVar + ' ' + self.publicVar;
}

, , :

var wWidget = new Widget();
wWidget.publicVar = "I have overridden the public property";
var strSummary = wWidget.publish(fnSummary);
var strDetails = wWidget.publish(fnDetails);
console.log(strSummary,strDetails);

, wWidget, . - , , , .

, SO , , , .

+4

# 3 , .

0

- , . , , , , (, ). . Javascript -, javascript . , , , C do. , , .

0

. , .

/, , .

, , :

public void Run()
{
    var template = "{0} inches is {1} centimetres.";
    Console.WriteLine(template, 2.0, 2.0 * 2.54);
    Console.WriteLine(template, 5.0, 5.0 * 2.54);
}

, * 2.54, ​​:

private double Inches2Centimetres(double inches)
{
    return inches * 2.54;
}

:

public void Run()
{
    var template = "{0} inches is {1} centimetres.";
    Console.WriteLine(template, 2.0, Inches2Centimetres(2.0));
    Console.WriteLine(template, 5.0, Inches2Centimetres(5.0));
}

, , , , .

, :

public void Run()
{
    Func<double, double> inches2Centimetres = inches => inches * 2.54;

    var template = "{0} inches is {1} centimetres.";
    Console.WriteLine(template, 2.0, inches2Centimetres(2.0));
    Console.WriteLine(template, 5.0, inches2Centimetres(5.0));
}

, Inches2Centimetres Inches2Centimetres . .

, Func<...>, lambdas , .

, : , , gumbf , , .

public static T[] UsingDataReader<T>(
    this IDbConnection @this,
    string query,
    Func<IDataReader, T> transform)
{
    //...
}

lamdba, Func<IDataReader, T> transform , , , T. , , . .

string[] names = conn.UsingDataReader<string>("select FirstName from People;",
    dr => dr["FirstName"] as string);

, , , .

0

, - . , ( ). , , .

Lambdas # - . , , .

"" , .

0

Source: https://habr.com/ru/post/1764813/


All Articles