Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe05.iad.POSTED!not-for-mail From: Arved Sandstrom User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Java 8 Streams and Eratosthenes References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit Lines: 77 Message-ID: X-Complaints-To: abuse@newsgroups-download.com NNTP-Posting-Date: Wed, 05 Jun 2013 20:43:20 UTC Organization: Public Usenet Newsgroup Access Date: Wed, 05 Jun 2013 17:43:19 -0300 X-Received-Bytes: 3355 Xref: csiph.com comp.lang.java.programmer:24313 On 06/05/2013 04:44 AM, Sebastian wrote: > Am 05.06.2013 09:03, schrieb Sebastian: >> Am 05.06.2013 00:35, schrieb Arved Sandstrom: >>> On 06/04/2013 06:54 PM, Sebastian wrote: >>>> Hello there, >>>> >>>> I'd like to start an open discussion on issues involving the Streams >>>> API. As an exercise, I have looked at generating the first n primes, >>>> for some given n, using the sieve of Eratosthenes. It seemed a >>>> plausible enough proposition, seeing that the sieve involves repeatedly >>>> filtering a sequence of integers. >>>> >> [snip] >>>> >>>> Perhaps the lesson is that some things are best not done using >>>> streams. >> [snip] >>>> >>>> What are your opinions? >>>> >>>> Best, >>>> Sebastian >>> [ SNIP ] >>> >>> Some things are best not done using Java. Given interop, I'd use Closure >>> or Scala for this type of stuff, if the main program is still Java. >>> >>> AHS >> >> That's a bit of a dodge, isn't it? Scala may be a nice language, but >> that doesn't help with investigating Java's strength and weaknesses. >> >> -- Sebastian > > Or perhaps it does. There are many Scala implementations of this > algorithm on the internet, cf. this one > > def primes : Stream[Int] = { > > var is = Stream from 2 > > def sieve(numbers: Stream[Int]): Stream[Int] = { > Stream.cons( > numbers.head, > sieve(for (x <- numbers.tail if x % numbers.head > 0) yield x)) > } > > sieve(is) > } > > from > http://louisbotterill.blogspot.de/2009/07/scala-lazy-evaluation-and-sieve-of.html > > > What the implementations all seem to have in common is the ability to > access the head of the stream and still lazily operate on the rest. It > is an ability that Java streams lack. So perhaps the way to go is to > create a sort of LazyLinkedList in Java (if there isn't one.) > > -- Sebastian > 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 -- When a true genius appears, you can know him by this sign: that all the dunces are in a confederacy against him. -- Jonathan Swift