Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Thinking in Lambdas

Raghavendra Desoju
Ranch Hand
Posts: 88
2
Hi,

I have a few questions on programming with lambdas. I have gone through couple of training materials online to understand about Lambdas.

I understand that it reduces the length of the code extensively depending on use cases.

I would like to ask a few questions:-

1. What is the best way to develop a mindset to induce lambdas when developing code from scratch?

2. What kind of thinking pattern is needed if we are enhancing/changing existing code?

Hope "Mastering Lambdas" book covers these details.

Thanks,
Raghu

Jason Bullers
Ranch Hand
Posts: 111
8
• 5
I'm not really sure what the "best way" to develop that mindset is, but for me, I find it helps to think about problems in terms of what I want to accomplish. The problems that lambdas are really good at solving elegantly are those in which you can put something in one end and massage it through a series of operations to get an end result. Those operations are the what. As an example (one that you've probably seen several times in the other materials you've read), imagine I have this goal in mind:

Sum the squares of the first five integers.

I need a for loop running from 1 to 5

Next I need to square each of those integers

Now I need a place to keep track of the sum

And finally, I need to sum those squares

There are a lot of details in that code that I had to think about, such as the loop and introducing the variable to hold the sum. None of this really provides any value with respect to the original problem statement; they're noise I have to write because I'm telling the computer how to go about the solution. If we stay more in line with the original problem statement and think about those steps in a more abstract way (the what), lambdas and the stream API are a much more natural fit:

I need the numbers 1 through 5

Next I need to square each of those integers

And finally, I need to sum those squares

Extending or changing existing code follows the same thinking: what is the code trying to accomplish? Can you take all of those detailed how steps and express the problem as a series of declarative operations instead of very detailed procedural ones?

Hope that helps a bit!

Campbell Ritchie
Sheriff
Posts: 50724
83
Jason Bullers wrote: . . . There are a lot of details in that code that I had to think about, such as the loop . . .
And there I was a couple of days ago telling somebody to use the “conventional form” of a for loop. That for loop has to deviate from the conventional form, using <= or putting the ++ operator in an unusual place or some other strangeness (not all strangenesses at once, though). Not only is that code detailed, but it is also error‑prone in case of out‑by‑one errors.

Not only is the code with the λ i -> i * i clearer, but it is also much robuster. The only likely error is that you might use range() instead of rangeClosed().

I am usually tight with cows but think your post merits one

Stephan van Hulst
Bartender
Posts: 6486
83
I agree, have another!

Winston Gutkowski
Bartender
Posts: 10527
64
Jason Bullers wrote:Extending or changing existing code follows the same thinking: what is the code trying to accomplish? Can you take all of those detailed how steps and express the problem as a series of declarative operations instead of very detailed procedural ones?
Hope that helps a bit!

It certainly does. Have a cow. Best explanation I've seen so far (and by far).

And here was me thinking I was going to have to put on my thinking cap to tackle Lambdas, when in fact they're all about what I keep banging on at beginners to do - so much so that I even wrote a page about it (WhatNotHow).

Thanks a lot Jason!

Winston

Jason Bullers
Ranch Hand
Posts: 111
8
Thanks for the cows guys! Happy I could help.

Winston Gutkowski
Bartender
Posts: 10527
64
Jason Bullers wrote:Thanks for the cows guys! Happy I could help.

Mine'll be a bit late. I've reached my "cow quota" for the day, so you'll get it tomorrow.

Winston

Raghavendra Desoju
Ranch Hand
Posts: 88
2
Thanks a lot Jason for a nice explanation with an example. Yes, it helps a lot.

Regards,
Raghu

Maurice Naftalin
Author
Greenhorn
Posts: 20
6
• 1
Hi Raghu

Jason has provided a very nice explanation that doesn't leave me much to say. The one thing that I would add is that the functional style, which emphasizes what the program should do rather than how to do it, allows the implementation much more "wiggle room" for optimization. The example I use in the book is from everyday life:
If someone were to ask you to mail some letters with the instruction “repeat the following action: if you have any more letters, take the next one in alphabetical order of addressee’s surname and put it in the mailbox,” your kindest thought would probably be that they have overspecified the task. You would know that ordering doesn’t matter in this task, and neither does the mode—sequential or parallel—of execution, yet it would seem you aren’t allowed to ignore them.

Without the overspecification, an implementation would be free to choose parallel-mode execution. Actually, with Java 8 streams, you have say you want parallel execution, because the runtime can't work out whether that will be more efficient – you still have to do that. But it only becomes possible at all because you've given up the unnecessary ordering imposed by the for-loop.

Regards
Maurice

Raghavendra Desoju
Ranch Hand
Posts: 88
2
Thank you Maurice !! Eagerly waiting for your book !!

Phil Freihofner
Ranch Hand
Posts: 121
3
By far, of everything I've read, the best explanation of lambdas I've come across to date is in "JavaFX for Dummies" by Doug Lowe. The nice thing about his writing is that he keeps it all very concrete, taking a single use case and showing several different ways of coding it. He has a good sense of what a typical Java coder knows and needs to learn (assuming my case is "typical"). Most other explanations I've seen head straight for the theoretical nuances or omit examples about one or more of the ways to code them. It's part of Chapter Three, and a bit long to quote directly. But after reading it, I became quite comfortable with using lambdas, and feel like I can now follow the more abstract explanations about them as well. By the way, it is a good book overall. Lowe seems to have a knack for knowing what to present and in what order. I'm doing well with picking up JavaFX overall.