19 Apr 2013, 01:06
Generic-user-small

Jason Weden (3 posts)

Venkat, great book. I’ve skimmed the whole thing and now am going into more depth. As of yet, I haven’t seen any example of a nested function shown as an alternative to a private method. As you know from Scala, this makes code more readable since we don’t have to try and figure out who is using the private method as the nested function is local within a method.

Is it not a good idea in Java 8 to create a lambda expression inside a method and invoke it therein instead of having the logic in the lambda pulled out to a private method?

19 Apr 2013, 15:42
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Hi Jason, thank you for your comments and feedback.

You make a good point and this would be a good feature.

I wonder if the example on page 26 (PickElementsMultipleCollection.java) fits the bill? The Predicate is a local lambda within the method. There are a couple on page 27 also. Would you suggest something that stands out more?

Thanks

Venkat

19 Apr 2013, 18:55
Generic-user-small

Jason Weden (3 posts)

Yes, that is pretty close. Those examples show passing a lambda to a method as a parameter. But I was thinking of something a little more explicit where we call a function multiple times from a within a method to only show how one can invoke the same code in a lambda multiple times without creating a separate private method. And it is worth mentioning that this is a NICE SUBSTITUTE to scoping logic to an outside private method.

See my blog post here: http://jweden.tumblr.com/post/42608766021/the-power-of-java-8-lambdas-with-generics

Also let me know if any of this is a bad idea. ;-)

22 Apr 2013, 11:21
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Thanks Jason, I will give it some thoughts.

16 Jun 2013, 14:45
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Hi Jason, finally managed to get back to editing the book. The use case you describe, I think, is in the Camera.java example, with printCaptured serving the purpose you mentioned. Thanks again for your input.

Regards,

Venkat

01 Aug 2013, 17:14
Generic-user-small

Jason Weden (3 posts)

Awesome! Thanks for the “props” in the Acknowledgements too!

  You must be logged in to comment