Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes)
On 06/09/2013 08:27 PM, Sebastian wrote:
Am 05.06.2013 22:43, schrieb Arved Sandstrom:
On 06/05/2013 04:44 AM, Sebastian wrote:
[snip]
Me I wouldn't re-invent the wheel in this case. Implementing this type
of laziness is not that straightforward.
And why not use interop between JVM languages? This way you leverage the
strengths. I usually have Java as the main language, but for some
functionality in some apps I use Scala. Maintainability goes up, for
starters, as does reliability and speed of development.
It's all ultimately JVM bytecode.
AHS
It's not everyone has a management who sees it this way. In our large
project (we're talking several hundred developers and a really
complicated build process), I don't see anyone advocating more
complexity by adding another compiler and language.
Anyway, it's turned out not to be too complicated in straight Java.
One can simply extend a straightforward implementation of the usual
cons-list with the following features, giving one a
FilterableConsList<T>:
a) accept a Function<T> to generate the elements in the tail
in sequence
b) accept a Supplier<FilterableConsList<T>> to supply a tail
c) accept a Predicate<T> to filter out non-matching elements
from the tail
and make the implementation class LazyConsList<T> lazy for the tail
(but eager for the head).
It's also easy to implement AbstractCollection by providing an iterator
and make the thing streamable by providing a spliterator of unknown
size.
With that, the driver for generating the first 100 primes becomes:
LazyConsList<Integer> s = sieve(LazyConsList.newList(2, x -> x + 1));
s.stream().limit(100).forEach(x->System.out.print(x + " "));
where the interesting sieve() method is defined like this:
private LazyConsList<Integer> sieve(FilterableConsList<Integer> s) {
Integer p = s.head();
return cons(p, () -> sieve(s.tail().filter(x -> x % p != 0)));
}
Now I think that looks pretty neat (and almost exactly like the
corresponding Scala code.)
-- Sebastian
It does look reasonably nice, I must admit. :-) I'll have to return to
Java 8 experiments and I think I'll start with a variant of your code.
I hear you when you talk about a large project with hundreds of
developers. I've worked in a number of environments like that, and in my
experience that kind of management myopia, fear and ignorance creeps in
with just a 10 or 20 person developer team, doesn't have to be hundreds.
Most managers I have encountered in close to 30 years of software
development have never programmed themselves; they are clueless. They
read in a book or article that having multiple languages in play is bad,
and avoid JNI or a second JVM language with interop like the
plague...but don't bat an eyelash at accepting entirely new technologies
or architectures that they can't find enough qualified people for. So
long as the entirely new stuff is based on Java I guess the working
assumption - hopelessly naive and ludicrous - is that any Java developer
will get it right away.
I am fortunate enough as a very senior consultant (very senior means
that you can converse about having used punched cards :-)) to mostly
work gigs now where I can dictate architecture, design and
implementation. I also now - where I can - avoid projects that involve
more than one architect/designer (who is also a skilled coder *), 2-3
developers, a great BA, and a good QA/QC person. I've found that a
skilled team this size outperforms a 20-30 person team easily, 9 times
out of 10.
More specifically, what really is the complexity of introducing some
Clojure or Scala compared to requiring that your developers master new
Java APIs? I've seen considerably more project problems due to imperfect
understanding of JPA 1.x/2.x, or IO/NIO, or web services and WSDLs, or
RMI, or concurrency - all Java - than any problems caused by
implementing logic in a language better suited for it than Java.
AHS
* Not usually the case in my experience. I have no idea what process
anoints a majority of the "architects" out there, but it's not
programming skills.
--
When a true genius appears, you can know him by this sign:
that all the dunces are in a confederacy against him.
-- Jonathan Swift