Skip to content

Instantly share code, notes, and snippets.

@tjweir
Created January 21, 2009 16:51
Show Gist options
  • Select an option

  • Save tjweir/50035 to your computer and use it in GitHub Desktop.

Select an option

Save tjweir/50035 to your computer and use it in GitHub Desktop.
Process HTML in scala
/*From: http://www.nabble.com/More-elegant-way-of-reading-HTML-from-a-URL-than-this--to21576710.html
*/
class URLLineReader(url: String) {
val reader = new java.io.BufferedReader(new java.io.InputStreamReader(new java.net.URL(url).openStream(), "US-ASCII"));
def foldLeft[T](init: T)(f: (T, String) => T): T = reader.readLine match {
case null => init
case line => foldLeft(f(init, line))(f)
}
}
object Main {
def main(args: Array[String]) = println(new URLLineReader("http://www.yahoo.com/").foldLeft("")(_ + _))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment