Skip to content

Instantly share code, notes, and snippets.

@manjuraj
Last active February 19, 2019 03:49
Show Gist options
  • Save manjuraj/9296160 to your computer and use it in GitHub Desktop.
Save manjuraj/9296160 to your computer and use it in GitHub Desktop.

Revisions

  1. manjuraj revised this gist Jun 29, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.scala
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    // Purpose:
    // - Ensures that a resource is deterministically disposed of once it goes out of scope
    // - Use this pattern when working with resources that should be closed or managed after use
    //
    // The benefit of this pattern is that it frees the developer from the responsibility of
    // explicitly managing a resources
    // explicitly managing resources

    import scala.io.Source
    import java.io._
  2. manjuraj revised this gist Jun 29, 2014. 1 changed file with 12 additions and 2 deletions.
    14 changes: 12 additions & 2 deletions gistfile1.scala
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    // Purpose:
    // - Ensure that a resource is deterministically disposed of once it goes out of scope
    // - Ensures that a resource is deterministically disposed of once it goes out of scope
    // - Use this pattern when working with resources that should be closed or managed after use
    // The benefit of this pattern is that it frees the developer from the responsibility of
    // explicitly managing a resources

    import scala.io.Source
    import java.io._
    @@ -44,4 +47,11 @@ def withPrintWriter[A](name: String, append: Boolean = true)(func: PrintWriter =

    withPrintWriter("greetings.txt") { writer =>
    writer.println("hello world!")
    }
    }


    // Flattening a Loan Pattern
    //
    // When loaning several objects, one often ends up with nested functions which can
    // be flattened using the pattern here:
    // http://www.casualmiracles.com/2012/04/18/flattening-the-loan-pattern/
  3. manjuraj revised this gist Mar 19, 2014. 1 changed file with 17 additions and 2 deletions.
    19 changes: 17 additions & 2 deletions gistfile1.scala
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@
    // - Ensure that a resource is deterministically disposed of once it goes out of scope

    import scala.io.Source
    import java.io.{FileWriter, PrintWriter, BufferedWriter}

    import java.io._
    def withFileIterator[A](name: String, encoding: String = "UTF-8")(func: Iterator[String] => A): A = {
    val source = Source.fromFile(name, "UTF-8")
    val lines = source.getLines()
    @@ -18,6 +18,21 @@ withFileIterator("greetings.txt") { lines =>
    lines.length
    }

    def withFileLine[A](name: String, encoding: String = "UTF-8")(func: String => A): Unit = {
    val in = new BufferedReader(new InputStreamReader(new FileInputStream(name), encoding))
    try {
    var line = in.readLine
    while (line != null) {
    func(line)
    line = in.readLine
    }
    } finally {
    in.close
    }
    }

    withFileLine("greetings.txt")(println)

    def withPrintWriter[A](name: String, append: Boolean = true)(func: PrintWriter => A): A = {
    val writer = new PrintWriter(new BufferedWriter(new FileWriter(name, append)))
    try {
  4. manjuraj revised this gist Mar 19, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.scala
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    import scala.io.Source
    import java.io.{FileWriter, PrintWriter, BufferedWriter}

    def withFileIterator[A](name: String, encoding: String = "UTF-8")(func: Iterator[String] => A) = {
    def withFileIterator[A](name: String, encoding: String = "UTF-8")(func: Iterator[String] => A): A = {
    val source = Source.fromFile(name, "UTF-8")
    val lines = source.getLines()
    try {
    @@ -18,7 +18,7 @@ withFileIterator("greetings.txt") { lines =>
    lines.length
    }

    def withPrintWriter[A](name: String, append: Boolean = true)(func: PrintWriter => A) = {
    def withPrintWriter[A](name: String, append: Boolean = true)(func: PrintWriter => A): A = {
    val writer = new PrintWriter(new BufferedWriter(new FileWriter(name, append)))
    try {
    func(writer)
  5. manjuraj created this gist Mar 1, 2014.
    32 changes: 32 additions & 0 deletions gistfile1.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    // Purpose:
    // - Ensure that a resource is deterministically disposed of once it goes out of scope

    import scala.io.Source
    import java.io.{FileWriter, PrintWriter, BufferedWriter}

    def withFileIterator[A](name: String, encoding: String = "UTF-8")(func: Iterator[String] => A) = {
    val source = Source.fromFile(name, "UTF-8")
    val lines = source.getLines()
    try {
    func(lines)
    } finally {
    source.close()
    }
    }

    withFileIterator("greetings.txt") { lines =>
    lines.length
    }

    def withPrintWriter[A](name: String, append: Boolean = true)(func: PrintWriter => A) = {
    val writer = new PrintWriter(new BufferedWriter(new FileWriter(name, append)))
    try {
    func(writer)
    } finally {
    writer.close()
    }
    }

    withPrintWriter("greetings.txt") { writer =>
    writer.println("hello world!")
    }