What is a lambda (function)?

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

    Imagine that you have a restaurant with a delivery option and you have an order that needs to be done in under 30 minutes. The point is clients usually don't care if you send their food by bike with a car or barefoot as long as you keep the meal warm and tied up. So lets convert this idiom to Javascript with anonymous and defined transportation functions.

    Below we defined the way of our delivering aka we define a name to a function:

    // ES5 
    var food = function withBike(kebap, coke) {
    return (kebap + coke); 
    };
    

    What if we would use arrow/lambda functions to accomplish this transfer:

    // ES6    
    const food = (kebap, coke) => { return kebap + coke };
    

    You see there is no difference for client and no time wasting to think about how to send food. Just send it.

    Btw, I don't recommend the kebap with coke this is why upper codes will give you errors. Have fun.

提交回复
热议问题