Last active
August 29, 2015 14:18
-
-
Save Yuichiroh/515c241100d1e68e2522 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 java.io.PrintWriter | |
import scala.io.Source | |
import scala.util.continuations._ | |
trait ContextType[B] | |
def contextType[B]: ContextType[B] = null | |
def using[A <: AutoCloseable, B: ContextType](resource: A)(op: A => B) = { | |
try op(resource) | |
finally resource.close() | |
} | |
def resource[A <: AutoCloseable, B: ContextType](r: A) = shift(using[A, B](r)) | |
def withResources[B](op: => B@cps[B]) = reset(op) | |
withResources { | |
implicit val _ = contextType[Unit] | |
val w1 = resource(new PrintWriter("col1.txt")) | |
val w2 = resource(new PrintWriter("col2.txt")) | |
Source.stdin.getLines().map(_.split("\t")).foreach { | |
case Array(col1, col2, _*) => | |
w1.println(col1) | |
w2.println(col2) | |
case Array(col1) => w1.println(col1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment