On Thu, Dec 20, 2012 at 5:45 AM, HamsterofDeath <
h-s...@gmx.de> wrote:
> is there a simple way to fake a sequence that returns the same object at
> every index? i want to avoid streams since they would introduce a
> memory-"leak" here....
A circular stream doesn't leak (so say the prophets).
Sadly, no method in the standard library will give us a circular
stream. Let's write one:
Welcome to Scala version 2.10.0-RC5
scala> def circular[T](xs: Stream[T]): Stream[T] = {
| lazy val knot: Stream[T] = xs #::: knot
| knot
| }
circular: [T](xs: Stream[T])Stream[T]
scala> circular(Stream(1)).take(10).mkString
res0: String = 1111111111
scala> circular(Stream.from(1).map{x => println(x); x}.take(3))
1
res1: Stream[Int] = Stream(1, ?)
scala> res1.take(12).mkString
2
3
res2: String = 123123123123
--
Seth Tisue | Northwestern University |
http://tisue.net
developer, NetLogo:
http://ccl.northwestern.edu/netlogo/