Java Lambda Humor

I was experimenting with Lambdas in Java on IntelliJ IDEA… they have a sense of humor.

I was wondering how simple a lambda could get so…

final Supplier<Double> supplier = () -> 1d;
double value = supplier.get();

A lambda (supplier) that produces a number (1) from no input. Pretty simple. What will happen to the code if I refactor supplier to make it an inline expression?

double value = ((Supplier<Double>) () -> 1d).get();

Well… that’s kind of in line with casting and such. Not that nice looking… However, now the IDE tells me I can make a simplification… “Replace method call on lambda with lambda body…” let’s try that:

double value = 1d;

Yeah… hmmm… that’s a pretty simple “lambda” indeed… hah!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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