Skip to content

Commit fc9071a

Browse files
committed
Added another dataflow example from the wiki.
1 parent 6ecabb4 commit fc9071a

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

doc/dataflow_top_stock_calc.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
This program determines which stock had the highest price in a given year.
2+
It as an example from chapter 1 "Introduction", section 1.2 "What's Scala?" of the book
3+
[Programming Scala: Tackle Multi-Core Complexity on the Java Virtual Machine](http://pragprog.com/book/vsscala/programming-scala).
4+
5+
## What It Does
6+
7+
This program takes a list of one or more stock symbols and a year. It then concurrently
8+
obtains the relevant stock data from Yahoo's iChart service for each symbol. Once all
9+
the data has been retrieved the program determines which stock had the highest year-end
10+
closing price.
11+
12+
* A sample of the data can be downloaded [here](http://ichart.finance.yahoo.com/table.csv?s=AAPL&a=11&b=01&c=2008&d=11&e=31&f=2008&g=m).
13+
14+
#### The Ruby Code
15+
16+
```ruby
17+
require 'concurrent'
18+
require 'open-uri'
19+
20+
def get_year_end_closing(symbol, year)
21+
uri = "http://ichart.finance.yahoo.com/table.csv?s=#{symbol}&a=11&b=01&c=#{year}&d=11&e=31&f=#{year}&g=m"
22+
data = open(uri) {|f| f.collect{|line| line.strip } }
23+
price = data[1].split(',')[4]
24+
price.to_f
25+
[symbol, price.to_f]
26+
end
27+
28+
def get_top_stock(symbols, year, timeout = 5)
29+
stock_prices = symbols.collect{|symbol| Concurrent::dataflow{ get_year_end_closing(symbol, year) }}
30+
Concurrent::dataflow(*stock_prices) { |*prices|
31+
prices.reduce(['', 0.0]){|highest, price| price.last > highest.last ? price : highest}
32+
}.value(timeout)
33+
end
34+
35+
symbols = ['AAPL', 'GOOG', 'IBM', 'ORCL', 'MSFT']
36+
year = 2008
37+
38+
top_stock, highest_price = get_top_stock(symbols, year)
39+
40+
puts "Top stock of #{year} is #{top_stock} closing at price $#{highest_price}"
41+
```
42+
43+
#### The Scala Code
44+
45+
```scala
46+
//START:PART1
47+
import scala.actors._
48+
import Actor._
49+
50+
val symbols = List( "AAPL", "GOOG", "IBM", "JAVA", "MSFT")
51+
val receiver = self
52+
val year = 2008
53+
54+
symbols.foreach { symbol =>
55+
actor { receiver ! getYearEndClosing(symbol, year) }
56+
}
57+
58+
val (topStock, highestPrice) = getTopStock(symbols.length)
59+
60+
printf("Top stock of %d is %s closing at price %f\n", year, topStock, highestPrice)
61+
//END:PART1
62+
63+
//START:PART2
64+
def getYearEndClosing(symbol : String, year : Int) = {
65+
val url = "http://ichart.finance.yahoo.com/table.csv?s=" +
66+
symbol + "&a=11&b=01&c=" + year + "&d=11&e=31&f=" + year + "&g=m"
67+
68+
val data = io.Source.fromURL(url).mkString
69+
val price = data.split("\n")(1).split(",")(4).toDouble
70+
(symbol, price)
71+
}
72+
//END:PART2
73+
74+
//START:PART3
75+
def getTopStock(count : Int) : (String, Double) = {
76+
(1 to count).foldLeft("", 0.0) { (previousHigh, index) =>
77+
receiveWithin(10000) {
78+
case (symbol : String, price : Double) =>
79+
if (price > previousHigh._2) (symbol, price) else previousHigh
80+
}
81+
}
82+
}
83+
//START:PART3
84+
```

0 commit comments

Comments
 (0)