Skip to content

Instantly share code, notes, and snippets.

@jiminoc
Created September 16, 2011 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiminoc/1222605 to your computer and use it in GitHub Desktop.
Save jiminoc/1222605 to your computer and use it in GitHub Desktop.
Example of the loan pattern in Scala
/**
* here we're going to test the loan pattern using something we do all the time, iterate over files and work with
* the lines in those files. Java makes it a pain the arse to just read simple lines in a file, lots of set up
* code and resource management. The nice part of this pattern is once you create that set up code once you can
* use a nice clean API like the one seen below.
* You can see here we're passing in a function to "withFileIterator that accepts a "line" string, we then can
* work on that line string freely and the resource will be closed in a finally block behind the scenes.
*/
@Test
def loanPatternWithFileIterator() {
val myfilename = "/var/log/secure.log"
LoanPattern.withFileIterator(myfilename) {
line => {
println(line)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment