Created
September 16, 2011 17:21
-
-
Save jiminoc/1222605 to your computer and use it in GitHub Desktop.
Example of the loan pattern in Scala
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
/** | |
* 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