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" ...

Get Functional Programming: A PragPub Anthology now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.