Created
January 11, 2014 07:15
-
-
Save RaasAhsan/8368036 to your computer and use it in GitHub Desktop.
A very simple Writer monad written in Scala.
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
class Writer[A](runWriter: (A, String)) { | |
def apply(x: (A, String)): Writer[A] = new Writer[A](x) | |
def run = runWriter | |
def flatMap[B](x: A => Writer[B]): Writer[B] = { | |
val (a: A, b: String) = runWriter | |
val (y: B, z: String) = x(a).run | |
new Writer[B]((y, b + z)) | |
} | |
def map[B](f: A => B): Writer[B] = { | |
val (a: A, b: String) = runWriter | |
val (y: B, z: String) = f(a) | |
new Writer[B]((y, z)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment