What is a Lambda Function? - Montana Webmaster

What is a Lambda Function?

Introduction

Lambda sounds like a Greek sorority/fraternity with drunken brawls. I’ve looked up the programming term lambda before, but since the topic doesn’t come into my workday, it doesn’t stick. But, yesterday, a LinkedIn connection who is learning how to code sent me a Java code sample with an unfamiliar structure and an unfamiliar library function, or what seemed to be a library function: performOperation().

When I saw the function and code that went with it, my first thought was, “Why in the world would someone go to all that trouble for simple arithmetic? All the programming languages have simple + – * / operators. But, it was a marvelous opportunity to use my daily 15 minutes of research time. I didn’t get very far, and I realized that this was going to take a whole set of daily research slots. I also realized that, unless I wrote about it, I would forget everything I learned. I might forget anyway, but at least I can come back to it.

What Is a Function?

Photo of red bell pepper
Data

It’s no use learning about a lambda function, if you don’t know what a function is in software development. Although most languages I’ve come across use the word function, Java uses the word “method” which I think is just silly … and confusing. But, method or function, it’s a few lines of code that do a specific task. And, it’s like a kitchen tool that can be pulled out to use over and over.

Let’s say that we have a red pepper. You can call on various kitchen tools to do a task.

  1. Blender.Puree(RedBellPepper);
  2. Knife.Chop(RedBellPepper);
  3. GasFlame.Roast(RedBellPepper);

Puree(), Chop() and Roast() are the functions. You could also use other tools.

  1. FoodProcessor.Chop(RedBellPepper);
  2. Oven.Roast(RedBellPepper);

Or you could use those functions on some other ingredient.

  1. Blender.Puree(GreenBellPepper);
  2. Knife.Chop(GreenBellPepper);
  3. GasFlame.Roast(GreenBellPepper);

These are the “normal” patterns for pulling function code out of the drawer and using them in many languages. Notice that the lines of code start with an object: Blender, Knife, GasFlame and then follow that object name with something the object can do: Puree, Chop, Roast. Both the objects and the functions have names. The thing about lambda functions is that they don’t have names. But, performOperation() is a function … and it has a name. So, why is it called a lambda function?

What is a Lambda Function?

The problem with trying to look up the basic idea behind a lambda function is that the search results are how to’s for specific technologies, not explanations about the big picture. When I entered “what is a lambda function” in Google, the first link is to AWS, the next two are for Python, and one for C#. The one reference to Wikipedia calls it an “anonymous function”, which is what it is, but not everyone who searches would know that.

“In computer programming, an anonymous function (function literallambda abstractionlambda functionlambda expression or block) is a function definition that is not bound to an identifier. ” Let’s see if we can interpret that into English. The functions above have names: Puree, Chop, Roast. But, even if a knife didn’t have a name, it could still chop. It would just be much more difficult for one person to say to the other, “Please pass me the X.” We could use X for everything that could be passed, if they are right in front of you, but not so much if they are somewhere else.

That is Lambda. It’s running code that is right there It’s not stored somewhere else, where it needs a name to identify it and use it.

Simple Lambda Examples

Python

  1. multiply = lambda a : a + 10
    print(multiply(5))
  2. multiply = lambda a, b : a * b
    print(multiply(56))
  3. multiply = lambda a, b, c : a + b + c
    print(multiply(562))

This example is from W3Schools. See link in References.

JavaScript

const multiply = (a, b) => {
return a * b;}

Note some differences and similarities between the use of lambda in Python and in javaScript:

  1. Python uses the word lambda, JavaScript does not.
  2. Both actually do use a variable: multiply.
  3. In JavaScript lambda’s can have, but don’t need, a return statement. The return action happens in Python without a return statement.

Questions

  1. If a lambda is unnamed, why does it have a name?

References

C++ Examples

Wikipedia page

Python Lambda

JavaScript Lambda

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.