Scala View + Stream combo causing OutOfMemory Error. How do I replace it with a View? -


i looking @ solving simple problem, eratosthenes sieve, using idiomatic scala, learning purposes.

i've learned stream caches, not performant when determining nth element because it's o(n) complexity access memoisation of data, therefore not suitable situation.

    def primes(nums: stream[int]): stream[int] = {         stream.cons(nums.head,             primes((nums tail) filter (x => x % nums.head != 0)))     }      def ints(n: int): stream[int] = {         stream.cons(n, ints(n + 1))      };     def nthprime(n: int): int = {         val prim = primes(ints(2)).view take n tolist;         return prim(n - 1);     }; 

the integer stream problematic one. while prime number filtering done, jvm runs outofmemory. correct way achieve same functionality without using streams?

basically take view of primes view of ints , display last element, without memoisation?

i have had similar cases stream idea, did not need store it's values. in order consume stream without storing it's values created (what called) throwawayiterator:

class throwawayiterator[t](var stream: stream[t]) extends iterator[t] {   def hasnext: boolean = stream.nonempty   def next(): t = {     val next = stream.head     stream = stream.tail     next   } } 

make sure not store reference instance of stream passed in.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -