O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Revisiting an Earlier Example

Let’s put the parallel collection to a practical use. In Chapter 3, Scala and Functional Style, we looked at concise code to fetch and process stock prices from Yahoo. Let’s revisit that code and make it run parallel.

For this example, we’ll use the following tickers:

val tickers = List("AAPL", "AMD", "CSCO", "GOOG", "HPQ", "INTC", "MSFT", "ORCL")

The code to fetch the price and construct it into a StockPrice object is repeated from that chapter here.

 case​ ​class​ StockPrice(ticker ​:​ ​String​, price ​:​ ​Double​) {
 def​ print ​=​ println(​"Top stock is "​ + ticker + ​" at price $"​ + price)
 }
 def​ getPrice(ticker ​:​ ​String​) ​=​ {
 val​ url ​=​ s​"http://download.finance.yahoo.com/d/quotes.csv?s=${ticker}&f=snbaopl1" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required