2017-12-27 387 views
0

我對斯卡拉很新,我必須找到範圍[i; j]中數字的素數。 這是我如何填寫流:迭代通過流

val stream = (i to j).toStream 

,然後我把這個函數應該找到素數:

def findPrimes(s: Stream[Int]): Stream[Int] = { 
    println("HEAD: " + s.head) 
    return findPrimes(s.tail) 
// s.head #:: findPrimes(s.tail.filter(_ % s.head != 0)) 
    } 

1.4.3流去安慰,我很奇怪爲什麼我收到異常:

Exception in thread "main" java.util.NoSuchElementException: head of empty stream 
    at scala.collection.immutable.Stream$Empty$.head(Stream.scala:1104) 
    at scala.collection.immutable.Stream$Empty$.head(Stream.scala:1102) 
    at com.example.anna.app.HelloWorld$.findPrimes(HelloWorld.scala:43) 
STREAM SIZE IS 100 
    at com.example.anna.app.HelloWorld$.prime(HelloWorld.scala:32) 
HEAD: 1 
    at com.example.anna.app.HelloWorld$.delayedEndpoint$com$example$anna$app$HelloWorld$1(HelloWorld.scala:11) 
HEAD: 2 
HEAD: 3 
    at com.example.anna.app.HelloWorld$delayedInit$body.apply(HelloWorld.scala:3) 
HEAD: 4 
    at scala.Function0.apply$mcV$sp(Function0.scala:34) 
HEAD: 5 
HEAD: 6 
    at scala.Function0.apply$mcV$sp$(Function0.scala:34) 
HEAD: 7 
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) 
HEAD: 8 
HEAD: 9 
    at scala.App.$anonfun$main$1$adapted(App.scala:76) 
HEAD: 10 
HEAD: 11 
    at scala.collection.immutable.List.foreach(List.scala:389) 
HEAD: 12 
    at scala.App.main(App.scala:76) 
HEAD: 13 
    at scala.App.main$(App.scala:74) 
HEAD: 14 
HEAD: 15 
    at com.example.anna.app.HelloWorld$.main(HelloWorld.scala:3) 
HEAD: 16 
    at com.example.anna.app.HelloWorld.main(HelloWorld.scala) 
HEAD: 17 

從17開始到最後沒有一個例外。 而且這個註釋行不起作用 - 它會拋出相同的異常。

任何幫助&意見將不勝感激。

回答

1

您傳遞給findPrimes的數據流的大小有限,當您撥打findPrimes時會流失。

使用Stream.from方法創建無限流。

斯卡拉REPL

scala> :paste 
// Entering paste mode (ctrl-D to finish) 


def primeStream(s: Stream[Int]): Stream[Int] = 
    Stream.cons(s.head, primeStream(s.tail filter { _ % s.head != 0 })) 
val primes = primeStream(Stream.from(2)) 


// Exiting paste mode, now interpreting. 

primeStream: (s: Stream[Int])Stream[Int] 
primes: Stream[Int] = Stream(2, ?) 

scala> primes.take(100).toList 
res0: List[Int] = List(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541) 

問題與您的代碼。

scala> val primes = primeStream((1 to 100).toStream) 
primes: Stream[Int] = Stream(1, ?) 

scala> primes.take(100).toList 
java.util.NoSuchElementException: head of empty stream 
    at scala.collection.immutable.Stream$Empty$.head(Stream.scala:1104) 
    at scala.collection.immutable.Stream$Empty$.head(Stream.scala:1102) 
    at .primeStream(<console>:12) 
    at .$anonfun$primeStream$1(<console>:12) 
    at scala.collection.immutable.Stream$Cons.tail(Stream.scala:1169) 
    at scala.collection.immutable.Stream$Cons.tail(Stream.scala:1159) 
    at scala.collection.immutable.Stream.$anonfun$take$2(Stream.scala:789) 
    at scala.collection.immutable.Stream$Cons.tail(Stream.scala:1169) 
    at scala.collection.immutable.Stream$Cons.tail(Stream.scala:1159) 
    at scala.collection.generic.Growable.loop$1(Growable.scala:54) 
    at scala.collection.generic.Growable.$plus$plus$eq(Growable.scala:58) 
    at scala.collection.generic.Growable.$plus$plus$eq$(Growable.scala:50) 
    at scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:186) 
    at scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:44) 
    at scala.collection.TraversableLike.to(TraversableLike.scala:590) 
    at scala.collection.TraversableLike.to$(TraversableLike.scala:587) 
    at scala.collection.AbstractTraversable.to(Traversable.scala:104) 
    at scala.collection.TraversableOnce.toList(TraversableOnce.scala:294) 
    at scala.collection.TraversableOnce.toList$(TraversableOnce.scala:294) 
    at scala.collection.AbstractTraversable.toList(Traversable.scala:104) 
    ... 28 elided 
+0

但我怎樣才能限制流,並把數字只在範圍[我; j]?在這種情況下 - 我不需要100個第一素數,我需要找到範圍內的所有素數並返回第n個。 – alalambda

+0

如果你把無限流,你可以涵蓋所有情況。無限流實際上是有限的。它不會佔用額外的內存。 – pamu

1

爲什麼會引發異常?原因是在println聲明中調用空流的頭部方法。

所以我們可以模式匹配這個流返回空流時尾部空如下(有更好的方法來遍歷流,但我想不會改變你的代碼非常多):

def findPrimes(s: Stream[Int]): Stream[Int] = s match { 
     case Stream.Empty => 
      println("END") //consider removing println statements 
      Stream.Empty 
     case h #:: tl => 
      println(s"HEAD: $h") 
      h #:: findPrimes(tl) 
    }