What is a lambda (function)?

后端 未结 23 2197
太阳男子
太阳男子 2020-11-22 04:47

For a person without a comp-sci background, what is a lambda in the world of Computer Science?

相关标签:
23条回答
  • 2020-11-22 05:00

    I have trouble wrapping my head around lambda expressions because I work in Visual FoxPro, which has Macro substitution and the ExecScript{} and Evaluate() functions, which seem to serve much the same purpose.

    ? Calculator(10, 23, "a + b")
    ? Calculator(10, 23, "a - b");
    
    FUNCTION Calculator(a, b, op)
    RETURN Evaluate(op)
    

    One definite benefit to using formal lambdas is (I assume) compile-time checking: Fox won't know if you typo the text string above until it tries to run it.

    This is also useful for data-driven code: you can store entire routines in memo fields in the database and then just evaluate them at run-time. This lets you tweak part of the application without actually having access to the source. (But that's another topic altogether.)

    0 讨论(0)
  • 2020-11-22 05:01

    Just because I cant see a C++11 example here, I'll go ahead and post this nice example from here. After searching, it is the clearest language specific example that I could find.

    Hello, Lambdas, version 1

    template<typename F>
    
    void Eval( const F& f ) {
            f();
    }
    void foo() {
            Eval( []{ printf("Hello, Lambdas\n"); } );
    }
    

    Hello, Lambdas, version 2:

    void bar() {
        auto f = []{ printf("Hello, Lambdas\n"); };
        f();
    }
    
    0 讨论(0)
  • 2020-11-22 05:01

    You can think of it as an anonymous function - here's some more info: Wikipedia - Anonymous Function

    0 讨论(0)
  • 2020-11-22 05:01

    It is a function that has no name. For e.g. in c# you can use

    numberCollection.GetMatchingItems<int>(number => number > 5);
    

    to return the numbers that are greater than 5.

    number => number > 5
    

    is the lambda part here. It represents a function which takes a parameter (number) and returns a boolean value (number > 5). GetMatchingItems method uses this lambda on all the items in the collection and returns the matching items.

    0 讨论(0)
  • 2020-11-22 05:02

    @Brian I use lambdas all the time in C#, in LINQ and non-LINQ operators. Example:

    string[] GetCustomerNames(IEnumerable<Customer> customers)
     { return customers.Select(c=>c.Name);
     }
    

    Before C#, I used anonymous functions in JavaScript for callbacks to AJAX functions, before the term Ajax was even coined:

    getXmlFromServer(function(result) {/*success*/}, function(error){/*fail*/});
    

    The interesting thing with C#'s lambda syntax, though, is that on their own their type cannot be infered (i.e., you can't type var foo = (x,y) => x * y) but depending on which type they're assigned to, they'll be compiled as delegates or abstract syntax trees representing the expression (which is how LINQ object mappers do their "language-integrated" magic).

    Lambdas in LISP can also be passed to a quotation operator and then traversed as a list of lists. Some powerful macros are made this way.

    0 讨论(0)
  • 2020-11-22 05:02

    In Javascript, for example, functions are treated as the same mixed type as everything else (int, string, float, bool). As such, you can create functions on the fly, assign them to things, and call them back later. It's useful but, not something you want to over use or you'll confuse everyone who has to maintain your code after you...

    This is some code I was playing with to see how deep this rabbit hole goes:

    var x = new Object;
    x.thingy = new Array();
    x.thingy[0] = function(){ return function(){ return function(){ alert('index 0 pressed'); }; }; }
    x.thingy[1] = function(){ return function(){ return function(){ alert('index 1 pressed'); }; }; }
    x.thingy[2] = function(){ return function(){ return function(){ alert('index 2 pressed'); }; }; }
    
    for(var i=0 ;i<3; i++)
        x.thingy[i]()()();
    
    0 讨论(0)
提交回复
热议问题