Skip to content

Instantly share code, notes, and snippets.

@caiorss
Last active November 7, 2016 23:31
Show Gist options
  • Save caiorss/71ffbba53441ca9b68f05948c74eb9b3 to your computer and use it in GitHub Desktop.
Save caiorss/71ffbba53441ca9b68f05948c74eb9b3 to your computer and use it in GitHub Desktop.
Print European Central Bank Exchange Rates in Scala - Parse and download XML
import scala.xml._
val xml = XML.load("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
val rates = (xml.child \\ "@rate").map(_.text.toFloat)
val currencies = (xml.child \\ "@currency").map(_.text)
val usdRate = currencies.zip(rates).find(t => t._1 == "USD").get._2
currencies
.zip(rates)
.foreach(t => println("%s\t\t%.3f".format(t._1, t._2 / usdRate)))
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.xml._
val xml = XML.load("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
val rates = (xml.child \\ "@rate").map(_.text.toFloat)
val currencies = (xml.child \\ "@currency").map(_.text)
val usdRate = currencies.zip(rates).find(t => t._1 == "USD").get._2
currencies
.zip(rates)
.foreach(t => println("%s\t\t%.3f".format(t._1, t._2 / usdRate)))
// Exiting paste mode, now interpreting.
USD 1.000
JPY 104.022
BGN 1.796
CZK 24.810
DKK 6.831
GBP 0.817
HUF 283.289
PLN 3.963
RON 4.138
SEK 8.911
CHF 0.994
NOK 8.250
HRK 6.894
RUB 62.250
TRY 3.076
AUD 1.311
BRL 3.140
CAD 1.336
CNY 6.774
HKD 7.757
IDR 13018.997
ILS 3.862
INR 66.842
KRW 1133.312
MXN 18.572
MYR 4.173
NZD 1.397
PHP 48.262
SGD 1.392
THB 35.000
ZAR 13.880
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment