Skip to content

Instantly share code, notes, and snippets.

@joescii
Created March 27, 2014 10:40
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 joescii/9804738 to your computer and use it in GitHub Desktop.
Save joescii/9804738 to your computer and use it in GitHub Desktop.
Replicating Racket's file->lines
object FileReaders {
object file {
def ->(contents: Seq[String]) = contents
}
object lines {
def apply(path:String) = scala.io.Source.fromFile(path).getLines().toSeq
}
}
object RunMe extends App {
import FileReaders._
import java.io._
def makeTemp = {
val temp = File.createTempFile("FileReaders", "txt")
temp.deleteOnExit()
val writer = new PrintStream(temp)
writer.println("A first line")
writer.println("A second line")
writer.println("And a third for good measure")
temp
}
val temp = makeTemp
val path = temp.getCanonicalPath
val contents = (file->lines(path))
contents foreach (println(_))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment