Created
August 30, 2013 14:45
-
-
Save clayrat/6390623 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
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