Last active
September 24, 2017 16:21
-
-
Save waynejo/a417b4892626960111b032fff3c3b878 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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