Abstraction over future information


Palling around with Weather Underground 


import dispatch._, Defaults._case class Location(city: String, state: String)def weatherSvc(loc: Location) = {  host("api.wunderground.com") / "api" / "5a7c66db0ba0323a" /     "conditions" / "q" / loc.state / (loc.city + ".xml")}



val nyc = Location("New York", "NY")for (str <- Http(weatherSvc(nyc) OK as.String))  println(str)


Parsing XML 


def weatherXml(loc: Location) =  Http(weatherSvc(loc) OK as.xml.Elem)


Traversing XML 


def printer = new scala.xml.PrettyPrinter(90, 2)for (xml <- weatherXml(nyc))  println(printer.format(xml))


def extractTemp(xml: scala.xml.Elem) = {  val seq = for {    elem <- xml \\ "temp_c"  } yield elem.text.toFloat  seq.head}

Temperature of the future 

With this we can create another high-level access method:

def temperature(loc: Location) =  for (xml <- weatherXml(loc))    yield extractTemp(xml)


val la = Location("Los Angeles", "CA")for (t <- temperature(la)) println(t)
