|Java 8 Lambdas|
Author: Richard Warburton
Lambdas are a nice addition to Java, but how much do you need to know to make use of them?
The idea of a Lambda is quite easy. A Lambda is function that you can pass to a method as a parameter. The reason that this is a big deal in Java is that traditionally Java is strongly object oriented and the only thing that you can pass as a parameter to a method is an object. In Java the object is the basic unit of everything and you can't pass a function because it isn't an object. However, in Java 8 you can pass a function as long as it is a Lambda function. As you might guess, given Java's welcome obsession with backward compatibility, a Lambda isn't just a disconnected function that has nothing to do with objects or classes. In fact a lambda is an instance of a functional interface and if you recall an interface is a sort of class without any implementation.
This is more or less what you are told in the first two chapters of this book only not quite so directly. Just like the account I've just given, if you don't know what an interface is or are slightly vague on any of the Java ideas used in the examples you aren't going to get any help from the book. Not only this, but you aren't going to find a concise and precise definition of what a Lambda is or even the syntax for defining one. What you do get are a few examples from which you are supposed to deduce what all Lambda's look like. The discussion is like reading a chat with a Java expert - which if fine, but you can't interrupt to ask a question if it covers something you are not sure about. If you are the sort of programmer who likes a precise definition of what is being discussed then you might well find this book irritating.
There is a second potential irritation - it doesn't just cover Lambdas and their direct utilisation it spreads itself to cover all of the related technologies. The subtitle gives you a clue as to why the book is wider ranging than just Lambdas - "Functional Programming For the Masses". A title that might just upset an some sensitive folk. First off the style of functional programming described is at best utilitarian and doesn't really convey the theoretical purity of the idea. Secondly can proper functional programming ever be for the "masses"? And if it can isn't this an insult to both functional programming and the readers of this book? As long as you can remain laid back about it - well, you sort of understand what the author is trying to do. The subtitle would be better as "A few bits of functional programming in Java 8 that might make your code less verbose and easier to understand."
Chapter 3 moves on to consider Streams and all their associated new operations - map, filter, reduce and so on. This is where we meet some of the basic ideas of functional programming. Again the presentation is very discursive and there are not definitive definitions - you are expected to deduce what things look like or go and look it up online.
Chapter 4 looks a how to use the new features within a library and it introduces the idea of default methods and how not to reinvent multiple inheritance. Chapter 5 moves on to advanced collections and collectors taking the discussion of the new features beyond simple streams.
Chapter 6 pairs with Chapter 9 in that it covers data parallelism whereas Chapter 9 looks at concurrency. In fact the first section of Chapter 6 makes the distinction between parallelism and concurrency clear. The chapter then goes on to explain the use of parallel streams and what the advantages an problems are.
Chapter 7 discusses refactoring, testing and debugging in the light of lambdas. In particular where should you replace old code with a lambda? Again the approach is chatty and not definitive, in this case it probably isn't possible to be definitive anyway and there is lots of talk about patterns. Also covered are debugging lambdas and unit testing when you have lambdas. The next chapter patterns are to the fore and we look at general approaches to code construction when lambdas are available.
The final chapter brings us back to core usage of lambdas - as callbacks. Concurrency with lambdas is basically about how you can now pass a function to act as a callback. Of course, this is just a less verbose way of providing a callback and we still have all of the problems that callbacks create - callback hell and so on. There is some coverage of possible solutions - futures, reactive programming and so on but really the solution is in the next, or next next version of Java.
As it points out in the introduction, this book is that it is not for the Java beginner; it conducts its business on the assumption that you have core Java SE skills and know what the author is talking about. It also doesn't provide a definitive specification or even explanation of anything that it covers - let alone lambdas. It is a wide ranging discussion of many of the new features that make Java 8 slightly more functionally oriented. If you are very nearly a Java expert you might find this book fun to read.
|Last Updated ( Friday, 08 April 2016 )|