Last active
February 19, 2019 03:49
-
-
Save manjuraj/9296160 to your computer and use it in GitHub Desktop.
Revisions
-
manjuraj revised this gist
Jun 29, 2014 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 resources import scala.io.Source import java.io._ -
manjuraj revised this gist
Jun 29, 2014 . 1 changed file with 12 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,8 @@ // 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 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/ -
manjuraj revised this gist
Mar 19, 2014 . 1 changed file with 17 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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._ 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 { -
manjuraj revised this gist
Mar 19, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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): 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): A = { val writer = new PrintWriter(new BufferedWriter(new FileWriter(name, append))) try { func(writer) -
manjuraj created this gist
Mar 1, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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!") }