Skip to content

Instantly share code, notes, and snippets.

@waynejo
Last active September 24, 2017 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waynejo/a417b4892626960111b032fff3c3b878 to your computer and use it in GitHub Desktop.
Save waynejo/a417b4892626960111b032fff3c3b878 to your computer and use it in GitHub Desktop.
import cats._
import cats.data._
import cats.implicits._
sealed trait StreamCell[T]
case class StreamNil[T]() extends StreamCell[T]
case class StreamValue[T](value: T, next: Eval[StreamCell[T]]) extends StreamCell[T]
object Main {
def take[T](n: Int, s: Eval[StreamCell[T]]): Eval[StreamCell[T]] = {
Eval.later {
n match {
case 0 =>
StreamNil[T]()
case _ =>
s.value match {
case StreamNil() =>
StreamNil[T]()
case StreamValue(value, next) =>
StreamValue[T](value, take(n - 1, next))
}
}
}
}
def toList[T](s: Eval[StreamCell[T]]):List[T] = {
s.value match {
case StreamValue(value, next) =>
value +: toList(next)
case _ =>
Nil
}
}
def main(args: Array[String]): Unit = {
val values = Eval.later {
StreamValue[Char]('H', Eval.later {
StreamValue[Char]('I', Eval.later {
StreamNil()
})
})
}
println(toList(take[Char](1, values)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment