Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic Haskell-like iterate in Scala?

In Haskell, I can get an infinite list of sequential function applications by calling:

iterate :: (A -> A) -> A -> [A]

Suppose in scala I have f(x: A): A. Is there a function that will result in a stream of sequential function applications? Like iter(f: A => A, x: A): Stream[A]?

like image 804
hilberts_drinking_problem Avatar asked Dec 05 '22 02:12

hilberts_drinking_problem


1 Answers

Yes, it's in the library already: Iterator.iterate

Iterator.iterate(1)(_ + 1).drop(100 * 100 * 100).take(10).toList
//> List(1000001, 1000002, 1000003, 1000004, 1000005,
         1000006, 1000007, 1000008, 1000009, 1000010)
like image 181
The Archetypal Paul Avatar answered Dec 30 '22 00:12

The Archetypal Paul