Skip to content

Instantly share code, notes, and snippets.

@clayrat
Created August 30, 2013 14:45
Show Gist options
  • Save clayrat/6390623 to your computer and use it in GitHub Desktop.
Save clayrat/6390623 to your computer and use it in GitHub Desktop.
case class TupoOI[A](a: A, inputs: Stream[String]) {
def copoint: A = a
def map[B](fun: A => B): TupoOI[B] = TupoOI(fun(a), inputs)
def coflatMap[B](fun: TupoOI[A] => B): TupoOI[B] = {
TupoOI(fun(this), inputs.tail)
}
override def toString = a.toString
}
object comonadTupoOI {
def a(x: String): Double = { readLine; x.toDouble }
def b(y: Double): Int = { readLine; y.toInt }
def fa(x: TupoOI[String]): Double = x.copoint.toDouble
def fb(y: TupoOI[Double]): Int = y.copoint.toInt
def main(args: Array[String]) {
println((a _ andThen b _)("1.1"))
import TupoOI._
val inputs = Stream(readLine, readLine)
println(TupoOI("1.1", inputs).coflatMap(fa).coflatMap(fb))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment