Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #24291

Re: Java 8 Streams and Eratosthenes

From Arved Sandstrom <asandstrom2@eastlink.ca>
Newsgroups comp.lang.java.programmer
Subject Re: Java 8 Streams and Eratosthenes
References <kolnme$g3e$1@news.albasani.net>
Message-ID <ZYtrt.80089$cD1.53480@newsfe29.iad> (permalink)
Organization Public Usenet Newsgroup Access
Date 2013-06-04 19:35 -0300

Show all headers | View raw


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.
>
> However, things were made difficult by the fact that Stream#findFirst()
> is a terminal (and short-circuiting) operation, so I couldn't use it
> to access the first stream element to incrementally build a result.
> The solution shown below uses an additional storage for these elements,
> and finds out which of the elements passing through the stream are new
> primes by counting them at each stage. This works, but it is certainly
> not something to show your functional programming friends.
>
> I'd be grateful for suggestions for improvement, or other solutions.
> I'd also like to know how this could be enhanced to generate an
> infinite stream of primes (i. e. when the upper bound n is unknown and
> new filters may need to be generated on the fly). I could imagine a
> solution if it were possible to "pipe" one stream into another, but that
> doesn't seem to be possible.
>
> Perhaps the lesson is that some things are best not done using
> streams. I also include a Java7 while-loop version of the same below.
> This is more concise, clearer and easily generalizes to an infinite
> stream. (However, it is eager, not lazy, and would take some more effort
> to convert to a lazy solution, probably involving Callbacks and perhaps
> losing its conciseness.)
>
> 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
-- 
When a true genius appears, you can know him by this sign:
that all the dunces are in a confederacy against him.
-- Jonathan Swift

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Java 8 Streams and Eratosthenes Sebastian <news@seyweiler.dyndns.org> - 2013-06-04 23:54 +0200
  Re: Java 8 Streams and Eratosthenes Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-04 19:35 -0300
    Re: Java 8 Streams and Eratosthenes Sebastian <news@seyweiler.dyndns.org> - 2013-06-05 09:03 +0200
      Re: Java 8 Streams and Eratosthenes Sebastian <news@seyweiler.dyndns.org> - 2013-06-05 09:44 +0200
        Re: Java 8 Streams and Eratosthenes Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-05 17:43 -0300
          Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) Sebastian <news@seyweiler.dyndns.org> - 2013-06-10 01:27 +0200
            Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-09 22:28 -0300
              Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) Sebastian <news@seyweiler.dyndns.org> - 2013-06-10 19:43 +0200
                Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) markspace <markspace@nospam.nospam> - 2013-06-10 13:18 -0700
                Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) Sebastian <news@seyweiler.dyndns.org> - 2013-06-11 23:27 +0200
                Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) Sebastian <news@seyweiler.dyndns.org> - 2013-06-12 10:32 +0200
                Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) Sebastian <news@seyweiler.dyndns.org> - 2013-06-12 14:37 +0200
                Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) markspace <markspace@nospam.nospam> - 2013-06-12 12:05 -0700
                Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) Sebastian <news@seyweiler.dyndns.org> - 2013-06-12 22:53 +0200
                Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) markspace <markspace@nospam.nospam> - 2013-06-15 15:18 -0700
                Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) Sebastian <news@seyweiler.dyndns.org> - 2013-06-16 22:52 +0200
                Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-16 21:56 -0300
                Re: Lazy primes with Java 8 (was Re: Java 8 Streams and Eratosthenes) Sebastian <news@seyweiler.dyndns.org> - 2013-06-18 23:18 +0200
  Re: Java 8 Streams and Eratosthenes markspace <markspace@nospam.nospam> - 2013-06-04 17:24 -0700
    Re: Java 8 Streams and Eratosthenes Sebastian <news@seyweiler.dyndns.org> - 2013-06-05 09:09 +0200
      Re: Java 8 Streams and Eratosthenes markspace <markspace@nospam.nospam> - 2013-06-05 09:45 -0700
        Re: Java 8 Streams and Eratosthenes Sebastian <news@seyweiler.dyndns.org> - 2013-06-05 21:32 +0200
    Re: Java 8 Streams and Eratosthenes Sebastian <news@seyweiler.dyndns.org> - 2013-06-05 09:47 +0200

csiph-web