• Post Reply Bookmark Topic Watch Topic
  • New Topic

internal versus external iteration  RSS feed

 
Rick Goff
Ranch Hand
Posts: 54
Clojure Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your new book, how do you present the value proposition of internal iteration, as supported by the functional model, over external iteration as we've been doing in Java development?

Thanks,
Rick
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't actually use these terms. Not everyone likes terminology. Here is how it starts out in chapter 2:

When you process a collection, you usually iterate over its elements and do some work with each of them. For example, suppose we want to count all long words in a book. First, let’s put them into a list:



Now we are ready to iterate:



What’s wrong with it? Nothing really—except that it is hard to parallelize the code. That’s where the Java 8 bulk operations come in. In Java 8, the same operation looks like this:



...

In this chapter, you will learn all about streams. Many people find stream expressions easier to read than the loop equivalents. Moreover, they can be easily
parallelized. Here is how you count long words in parallel:



If that doesn't sell the feature, I don't know what does :-)

Cheers,

Cay
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the way you explained it without using some of those terminologies is much easier to understand. My experience so far with articles/docs/books related to lambdas has been that even with simple examples like iterating over a loop and incrementing a counter, they tend to make the explanation so complex that you fail to see any value with the lambda expressions (except maybe replacing 4 to 5 lines of code with just a single line - which anyway to me has never been appealing).
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!