Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 797 Bytes

README.md

File metadata and controls

32 lines (25 loc) · 797 Bytes

Close Monad

Build Status

This is an experimental implementation. I suggest that you should not use in production!

How to use

$ sbt run

Example codes

implicit def closer[R <: Closeable]: Closer[R] = Closer(_.close())

(for {
  in     <- Close(new FileInputStream(getClass.getResource("/source.txt").getPath))
  reader <- Close(new InputStreamReader(in, "UTF-8"))
  buff   <- Close(new BufferedReader(reader))
  out    <- Close(new FileOutputStream("dest.txt"))
  writer <- Close(new OutputStreamWriter(out, "UTF-8"))
} yield {
  var line = buff.readLine()
  while (line != null) {
    writer.write(line + "\n")
    line = buff.readLine()
  }
}).run()