This page is no longer maintained — Please continue to the home page at www.scala-lang.org

[Q]filter probrem on Stream

2 replies
shuji yamamoto
Joined: 2009-06-17,
User offline. Last seen 42 years 45 weeks ago.

Hi.

I am thinking about filter probrem on Stream.
Just like that:
----------
scala> val a = Stream.from(1)
a: Stream[Int] = Stream(1, ?)
^

scala> a filter {_ > 10}
res2: Stream[Int] = Stream(11, ?)

scala> a filter {_ > 100000000}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.lang.String.replace(String.java:2048)
at
sun.jkernel.DownloadManager.getBootClassPathEntryForClass(DownloadManager.
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:293)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
at
scala.tools.nsc.Interpreter$Request.loadAndRun(Interpreter.scala:896)
at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:508)
at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:494)
at
scala.tools.nsc.InterpreterLoop.interpretStartingWith(InterpreterLoop.scal
at
scala.tools.nsc.InterpreterLoop.command(InterpreterLoop.scala:230)
at scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:142)
at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:298)
at
scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:141)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

-----------------

I am using scala as CG generatro of chaos dynamic system.
http://github.com/yamasushi/code4joy/tree/master/chaos/src/

So , I found that error use Stream as dynamic system.
http://github.com/yamasushi/code4joy/blob/master/chaos/src/ChaosStreamCa...

trait ChaosStreamCanvas extends ChaosStream
{
def canvasPointsFrom
(dropIter:Int,maxIter:Int)
(minmax:((Double,Double),(Double,Double)))
(canvas:Picture[_] with Canvas)
(pt0:(Double,Double) ) : Stream[(Int,(Int,Int))] = {
//
(Stream.from(-dropIter) zip (chaosSystem.from(pt0) map canvas.transform(
minmax ) )
) take ( dropIter + maxIter )
}
//
def generateCanvasPoints
(numTrajectory:Int)
(dropIter:Int,maxIter:Int)
(canvas:Picture[_] with Canvas)
(minmax:((Double,Double),(Double,Double)))
(op:(Int,(Int,Int)) => Unit) : Unit = {
//
initialPoints take(numTrajectory) foreach { pt0 =>
canvasPointsFrom(dropIter,maxIter)(minmax)(canvas)(pt0) foreach {ip=>
if(canvas.isPointVisible(ip._2)) op(ip._1,ip._2)
}
}
}
}

genratePointsCanvasPoints does filtering points of dynamic system using
*if* instead of *filter* .
On early version , I used "filter" points , and found it is not good idea.

Shuji Yamamoto @ Japan

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: [Q]filter probrem on Stream
It is not a problem with Stream itself, but with what you are expecting of it. Look at this:
scala> val a = Stream.from(1)a: Stream[Int] = Stream(1, ?)
scala> a.filter(_ > 10) res0: Stream[Int] = Stream(11, ?)
scala> ares1: Stream[Int] = Stream(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ?)
scala> a.filter(_ > 1000)res2: Stream[Int] = Stream(1001, ?)
scala> ares3: Stream[Int] = Stream(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 , 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 9...
scala> Stream.from(1) filter (_ > 10000000)res4: Stream[Int] = Stream(10000001, ?)
Stream is lazy, but it keeps that values it has computed, unless you let the garbage collector collect them. Since you have a reference to the beginning of the Stream, specifically, "a", it has to keep all the numbers. See in the last command that, if you do not have such a reference, they can be garbage collected.


2009/11/25 shuji yamamoto <yamashuu [at] k3 [dot] dion [dot] ne [dot] jp>
Hi.

I am thinking about filter probrem on Stream.
Just like that:
----------
scala> val  a = Stream.from(1)
a: Stream[Int] = Stream(1, ?)
               ^

scala> a filter {_ > 10}
res2: Stream[Int] = Stream(11, ?)

scala> a filter {_ > 100000000}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
       at java.lang.String.replace(String.java:2048)
       at sun.jkernel.DownloadManager.getBootClassPathEntryForClass(DownloadManager.
       at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:293)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
       at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
       at scala.tools.nsc.Interpreter$Request.loadAndRun(Interpreter.scala:896)
       at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:508)
       at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:494)
       at scala.tools.nsc.InterpreterLoop.interpretStartingWith(InterpreterLoop.scal
       at scala.tools.nsc.InterpreterLoop.command(InterpreterLoop.scala:230)
       at scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:142)
       at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:298)
       at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:141)
       at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)


-----------------

I am using scala as CG generatro of chaos dynamic system.
http://github.com/yamasushi/code4joy/tree/master/chaos/src/

So , I found that error use Stream as dynamic system.
http://github.com/yamasushi/code4joy/blob/master/chaos/src/ChaosStreamCanvas.scala

trait ChaosStreamCanvas extends ChaosStream
{
       def canvasPointsFrom
                               (dropIter:Int,maxIter:Int)
                               (minmax:((Double,Double),(Double,Double)))
                               (canvas:Picture[_] with Canvas)
                               (pt0:(Double,Double) ) : Stream[(Int,(Int,Int))] = {
               //
               (Stream.from(-dropIter) zip (chaosSystem.from(pt0) map canvas.transform( minmax ) )
               ) take ( dropIter + maxIter )
       }
       //
       def generateCanvasPoints
                               (numTrajectory:Int)
                               (dropIter:Int,maxIter:Int)
                               (canvas:Picture[_] with Canvas)
                               (minmax:((Double,Double),(Double,Double)))
                               (op:(Int,(Int,Int)) => Unit) : Unit = {
               //
               initialPoints take(numTrajectory) foreach { pt0 =>
                       canvasPointsFrom(dropIter,maxIter)(minmax)(canvas)(pt0) foreach {ip=>
                               if(canvas.isPointVisible(ip._2)) op(ip._1,ip._2)
                       }
               }
       }
}

genratePointsCanvasPoints does filtering points of dynamic system using *if* instead of *filter* .
On early version , I used "filter" points , and found it is not good idea.


Shuji Yamamoto @ Japan



--
Daniel C. Sobral

Veni, vidi, veterni.
shuji yamamoto
Joined: 2009-06-17,
User offline. Last seen 42 years 45 weeks ago.
Re: [Q]filter probrem on Stream

Thanks , I understand.

scala> Stream.from(1) filter (_ > 10000000)
res0: Stream[Int] = Stream(10000001, ?)

scala> Stream.from(1) filter (_ > 100000000)
res1: Stream[Int] = Stream(100000001, ?)

scala> Stream.from(1) filter (_ > 1000000000)
res2: Stream[Int] = Stream(1000000001, ?)

But, it is very slow on my PC ^^

Shuji Yamamoto @ Japan

On Wed, 25 Nov 2009 20:17:14 +0900, Daniel Sobral
wrote:

scala-user [at] listes [dot] epfl [dot] ch

> It is not a problem with Stream itself, but with what you are expecting
> of
> it. Look at this:
>
> scala> val a = Stream.from(1)
> a: Stream[Int] = Stream(1, ?)
>
> scala> a.filter(_ > 10)
> res0: Stream[Int] = Stream(11, ?)
>
> scala> a
> res1: Stream[Int] = Stream(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ?)
>
> scala> a.filter(_ > 1000)
> res2: Stream[Int] = Stream(1001, ?)
>
> scala> a
> res3: Stream[Int] = Stream(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
> 15, 16, 17, 18, 19, 20, 21
> , 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
> 40, 41, 42, 43, 44, 45, 46
> , 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
> 65, 66, 67, 68, 69, 70, 71
> , 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
> 90, 91, 92, 9...
>
> scala> Stream.from(1) filter (_ > 10000000)
> res4: Stream[Int] = Stream(10000001, ?)
>
> Stream is lazy, but it keeps that values it has computed, unless you let
> the
> garbage collector collect them. Since you have a reference to the
> beginning
> of the Stream, specifically, "a", it has to keep all the numbers. See in
> the
> last command that, if you do not have such a reference, they can be
> garbage
> collected.
>
>
>
> 2009/11/25 shuji yamamoto
>
>> Hi.
>>
>> I am thinking about filter probrem on Stream.
>> Just like that:
>> ----------
>> scala> val a = Stream.from(1)
>> a: Stream[Int] = Stream(1, ?)
>> ^
>>
>> scala> a filter {_ > 10}
>> res2: Stream[Int] = Stream(11, ?)
>>
>> scala> a filter {_ > 100000000}
>> Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
>> at java.lang.String.replace(String.java:2048)
>> at
>> sun.jkernel.DownloadManager.getBootClassPathEntryForClass(DownloadManager.
>> at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:293)
>> at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
>> at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
>> at
>> scala.tools.nsc.Interpreter$Request.loadAndRun(Interpreter.scala:896)
>> at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:508)
>> at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:494)
>> at
>> scala.tools.nsc.InterpreterLoop.interpretStartingWith(InterpreterLoop.scal
>> at
>> scala.tools.nsc.InterpreterLoop.command(InterpreterLoop.scala:230)
>> at
>> scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:142)
>> at
>> scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:298)
>> at
>> scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:141)
>> at
>> scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
>>
>>
>> -----------------
>>
>> I am using scala as CG generatro of chaos dynamic system.
>> http://github.com/yamasushi/code4joy/tree/master/chaos/src/
>>
>> So , I found that error use Stream as dynamic system.
>>
>> http://github.com/yamasushi/code4joy/blob/master/chaos/src/ChaosStreamCa...
>>
>> trait ChaosStreamCanvas extends ChaosStream
>> {
>> def canvasPointsFrom
>> (dropIter:Int,maxIter:Int)
>> (minmax:((Double,Double),(Double,Double)))
>> (canvas:Picture[_] with Canvas)
>> (pt0:(Double,Double) ) :
>> Stream[(Int,(Int,Int))] = {
>> //
>> (Stream.from(-dropIter) zip (chaosSystem.from(pt0) map
>> canvas.transform( minmax ) )
>> ) take ( dropIter + maxIter )
>> }
>> //
>> def generateCanvasPoints
>> (numTrajectory:Int)
>> (dropIter:Int,maxIter:Int)
>> (canvas:Picture[_] with Canvas)
>> (minmax:((Double,Double),(Double,Double)))
>> (op:(Int,(Int,Int)) => Unit) : Unit = {
>> //
>> initialPoints take(numTrajectory) foreach { pt0 =>
>>
>> canvasPointsFrom(dropIter,maxIter)(minmax)(canvas)(pt0) foreach {ip=>
>> if(canvas.isPointVisible(ip._2))
>> op(ip._1,ip._2)
>> }
>> }
>> }
>> }
>>
>> genratePointsCanvasPoints does filtering points of dynamic system using
>> *if* instead of *filter* .
>> On early version , I used "filter" points , and found it is not good
>> idea.
>>
>>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland