Lambda Expressions

后端 未结 4 1314
慢半拍i
慢半拍i 2021-02-14 01:17

Can somebody explain me lambda expressions & what they can be used for. I have googled for it & have a rough idea. most of the examples give c# code. How about lambda ex

4条回答
  •  温柔的废话
    2021-02-14 02:11

    There are actually two things called "lambda expressions", which are rather loosely related:

    1. Lambda expressions are fundamental part of lambda calculus and are closely related to functional programming

    2. In imperative languages, lambda expressions are usually synonyms for anonymous methods. In C#, for example you can pass lambda expression (ie. an expression itself, not just its result) as an argument:

    C#:

    someCollection.Apply (x => 2*x); // apply expression to every object in collection
    // equivalent to 
    someCollection.Apply (delegate (int x) { return 2 * X; });
    

    Having said that, C does not support anonymous methods. You can, however, use function pointers to achieve similar results:

    int multiply (int x)
    {
        return 2 * x;
    }
    
    ...
    collection_apply (some_collection, multiply);
    

提交回复
热议问题