13 Mar 2013, 12:46
Generic-user-small

Wacek Kusnierczyk (4 posts)

On p. 3 you give an example of the functional way of solving problems:

final double totalOfDiscountedPrices = prices.stream().map((Integer price) -> price * 0.9).sum();

Since you earlier made a comment on the procedural way, “Those of us working from home have to keep this code away from the eyes of kids aspiring to be programmers”, I think you might want to find a better example. The above is still not what you’d want to show to your kids ;) as there is a much simpler solution:

final double totalOfDiscountedPrices = 0.9 * prices.stream().sum();

An example where the components would need to be squared, say, would be more to the point:

final double totalOfSquared = prices.stream().map((Integer price) -> price * price).sum();

although it does not make much sense in the context of prices.

13 Mar 2013, 20:25
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Hi Wacek, nice point, I like the suggestion. Will make the change before the next beta release! Thanks.

16 Mar 2013, 14:20
Generic-user-small

Arjo Ouwens (1 post)

Little correction on a example in the book: page 34, Transform.java:

final List<String> uppercaseNames = new ArrayList<String>();
friends.forEach(name -> uppercaseNames.add(name.toUpperCase()));
System.out.println(uppercaseNames);

but this will give you a compiler warning: “incompatibel return type boolean in lambda expression”.

Simple correction:

final List<String> uppercaseNames = new ArrayList<String>();
friends.forEach(name -> uppercaseNames.add(0, name.toUpperCase()));
System.out.println(uppercaseNames);
17 Mar 2013, 03:01
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Hi Arjo, thank you for the suggestion.

I tried the first example code on both build 79 and build 81 (on the mac) and I am not getting any warning.

Could you help me with the settings you’re using so I can try to recreate it on my side.

Thanks.

Venkat

09 May 2013, 14:09
Generic-user-small

carlo (1 post)

Hi,

Reading your book which is great. Looking forward to the updates.

I tried the sample code with JDK 8 lambda build 88 and found that the sum() method on streams does not compile. Quite a few examples use sum().

I’m using Netbeans set to JDK 8 for source and libs.

public class DiscountFunctional { public static void main(final String[] args) { final double totalOfDiscountedPrices = prices.stream().map((Integer price) -> price * 0.9).sum();

System.out.println("Total of discounted prices: " + totalOfDiscountedPrices);   } }

Exception in thread “main” java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.util.stream.Stream.sum at fpij.DiscountFunctional.main(DiscountFunctional.java:20) Java Result: 1

Wonder if sum() has been removed from the Java 8 ? I didnt see it in the Javadoc. Maybe there is another way to do it ?

Thanks Carlo

16 May 2013, 12:03
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Hi Carlo, thank you for your comments. The API has evolved and I need to make another update. I plan to get the next beta out with updates in June. Thanks. — Venkat

21 May 2013, 06:19
Avatar_scrat_l__age_de_glace_by_aguabrial-d3fesov_pragsmall

Marc-Daniel Ortega (3 posts)

Hi, I could not resist coming to the forum for chapter 8. Reproducing the example I noticed that currying the isPriceLessThan method as in this interface could add more symmetry into the findHighPrice method as in this implementation

Please note that the findHighPrice method has been curried too in order to make the max value flexible.

Using a windows environment less handy for measurements of time of execution, I created my one HOF timed in order to be able to trace the time execution of a supplier like function

The invocation of timed grossly emulate a rudimentary delay behavior.

Thanks for the wonderful work, and be sure next time you hop to Europe I manage to get a physical copy in addition to the numeric edition so you can sign it :)

Mark

16 Jun 2013, 16:10
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Hi Mark,

Thank you very much for so many wonderful suggestions. I enjoyed updating the book with each one of them.

Will certainly have to catch up next time our locales align.

Cheers,

Venkat

05 Oct 2013, 08:56
Sven3_pragsmall

Sven Ehrke (1 post)

In the book ‘Groovy in Action’ the authors are using assertions instead of System.out.printlns which I think has the advantage that the code itself expresses what is going on and you do not have to write: “this code prints…” And for the examples using lists lambdas then even make testing easier and more readable.

Example: ` assertEquals( “Brian-Nate-Neal-Raju-Sara-Scott”, friends().stream().collect(joining(“-“)) ); `

15 Oct 2013, 00:25
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Hi Sven, thanks for the suggestion. Good point, but as a matter of style I prefer running a code and seeing the output when learning a new language or a new feature. The other approach certainly has merits, it’s purely a preference here on my part. Regards, Venkat

05 Nov 2013, 03:24
Generic-user-small

GREG HELTON (2 posts)

Venkat, I have b3, Sep 18 version. The book does not include the code or a link to the code for Prices class referenced from http://media.pragprog.com/titles/vsjava8/code/introduction/fpij/DiscountFunctional.java

Located the code for Prices class at http://media.pragprog.com/titles/vsjava8/code/introduction/fpij/Prices.java but maybe not everyone will find it.

Greg

26 Nov 2013, 06:19
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Greg, thanks. The code is listed on page 3 (it’s the first one line code on that page) and I left out the link with intention. Regards, Venkat.

20 Dec 2013, 13:16
Generic-user-small

Dan Talpau (2 posts)

Hi, I just finished Ch. 2 Using Collections and I think the reduce/collect concepts/methods could be explained a little bit better.

Compared to Clojure’s reduce() API, the one in Java 8 seems a little bit more complicated.

For me it was not obvious what difference between reduce and collect is. The Oracle docs made it more clear for me that collect() mutates the accumulated value, which might be important in some scenarios.

Also, the 3rd overloaded reduce() method that takes an identity, accumulator and combiner is the least intuitive and not mentioned in the book; it’s pretty well explained here why an accumulator and a combiner are needed: http://stackoverflow.com/questions/15787042/strange-reduce-method-group-in-jdk8-bulk-collection-operations-library

Basically I would have liked to “get” Java 8 reduce/collect API from the book and not having to check Oracle docs/stackoverflow. Although maybe they are obvious for others.

20 Dec 2013, 21:36
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Dan, thank you very much for your feedback and suggestion. Your timing is great, I will have a window of opportunity in the next few weeks to touch the book (it is in the middle of production phase) and will see what I can do to improve in this area. Thanks a bunch. Venkat

21 Dec 2013, 20:49
Generic-user-small

Dan Talpau (2 posts)

As I progressed through the book, I saw that in Ch. 3 the collect() method is very well explained (using collect and Collectors); to my previous comment is a bit redundant :)

23 Dec 2013, 02:56
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Dan, thanks! Your comment is quite helpful, I will place a forward reference to what’s ahead. Cheers. :) Venkat

  You must be logged in to comment