What is a lambda (function)?

后端 未结 23 2296
太阳男子
太阳男子 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:02

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

    string[] GetCustomerNames(IEnumerable 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.

提交回复
热议问题