Basically the same as @Sean Vieira, with more syntax like Haskell:
scala> def iterate[A]: (A => A) => A => Stream[A] = {
| f => x => x
| }
iterate: [A]=> (A => A) => (A => Stream[A])
scala> iterate[Int](_+1)(1) take 10 toList
res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
And interchanged fand xcan help the compiler:
scala> def iterate[A]: A => (A => A) => Stream[A] = {
| x => f => x
| }
iterate: [A]=> A => ((A => A) => Stream[A])
scala> iterate(1)(2+) take 10 toList
res3: List[Int] = List(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
source
share