Lambda Expressions in Java, Python, C#, C++

Adrian D. Finlay
4 min readNov 13, 2017

--

GfyCat

No, Lambda Expressions are not as complicated as Church’s Lambda Calculus. No need to worry.

Lambda Expressions are simply an expression of an anonymous function. This is a very important concept in Functional Programming and is something like a literal expression for a function (such as how 1020L may be interpreted as a long integer literal in certain languages). This is a widespread, cornerstone concept in Functional Programming that has been added to OOP/Procedural centered languages like Java to reap some of the benefits of functional programming, which is useful for things like processing data. It also provides neat, concise syntax for implementing functions on the fly. They are based of off functional programming concepts used in the construction of expression trees.

In this article, I will demonstrate basic use among some common, popular languages. Instead of engaging in a tutorial of lambda expressions in each language, I will instead simply show the same example in various languages. Note also that I will not cover a language’s full coverage of Lambda Expressions (or the equivalent) rather, some basic use to get your feet wet.

I decided to piggyback off of Quincy Larson’s recommendation to use Medium’s code blocks but because they are visually unappealing to me and an utter nuisance to indent, I will not be using them again. This is, of course, not the fault of Quincy Larson. I have other small issues with this format but it is an OK start for small snippets. I will perhaps, as Larson also recommends, resort to GitHub gists.

Let’s start with Java.

Java

Java provides support for Lambda Expressions via Functional Interfaces. Let’s take a peek. Do note that because of my qualms with the format, I have not included the full source below. Look towards the end of the article for the full source.

import java.util.Scanner; //For Scanner class
import java.lang.Math; //For pow(), min()
import java.util.InputMismatchException; //InputMismatchException class
import static java.lang.System.out; //For println(), print()
//Defines Structure for Lambda Expression
@FunctionalInterface
interface MathOp {
//Must contain one and only one abstract method
double binaryMathOp(double a, double b);
}
//Main Class
public class Lambda {
//Main method
public static void main (String[] args) {
//Input Stream
Scanner in = new Scanner(System.in);
//Lambda Reference, Arithmetic Expressions
MathOp doMath;
double expr1=0, expr2 =0;
//Header
out.println("\nLet's demonstrate the usefulness of Airthmetic\n" +
"operations by using Lambda Expressions.\n");
//Prompt for Division
out.print("LET'S DO SOME DIVISION:\t");
try{
expr1 = in.nextDouble();
expr2 = in.nextDouble();
} catch (InputMismatchException e) {
out.println("\n\nYou have entered invalid input." +
"Restarting.\n\n");
main(args);
}
//Lambda Expression for Division
doMath = (x,y) -> { return x / y; };

//Print Result
out.println("The result is:\t" + doMath.binaryMathOp(expr1, expr2));

//Method References
// .... <Omitted> //Prompt to find minimum value
out.print("\nLET'S FIND THE MINIMUM VALUE:\t");
try{
expr1 = in.nextDouble();
expr2 = in.nextDouble();
} catch (InputMismatchException e) {
out.println("\n\nYou have entered invalid input." +
"Restarting.\n\n");
main(args);
}
//Method Reference (Lambda Expression)
doMath = Math::min;
//Print Result
out.println("The result is:\t" + doMath.binaryMathOp(expr1, expr2) + "\n");
//Close Input Stream
in.close();
}
}

Python

Lambda Expressions work a little differently in python. In python, Lambda Expressions are essentially anonymous functions. Also, note that unlike Java, we can define it’s behavior on the fly in the same way.

C#

In C#, Lambda Expressions are anonymous functions that can be used to create expression trees or delegates. We will discuss creating a delegate, which is something like a function pointer. This is much closer to the nature of a Lambda Expression than that of Java and Python.

C++

In C++, Lambda Expressions are closures, that is, they are an unnamed function object. You can use this function on the fly.

Want the source? Grab it here.

Like Lambdas? Let me know in the comments below :)

Looney Tunes Ending [4]

Interested in Java? Join my Java group on Facebook:

Like my Content? Subscribe to my mailing list:

Don’t forget to give it a…. ;)

IEmoji.com

--

--

Adrian D. Finlay

@thewiprogrammer. Lover of learning, programming. Tech writer, Java aficionado. Proud mango, fishing, NBA addict! & more. Network w/ me @ bit.ly/AdfNtWk