Created
September 24, 2013 21:03
-
-
Save dmalikov/6691197 to your computer and use it in GitHub Desktop.
nice constructor syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.Net | |
type Stock(symbol : string) = class | |
let url = | |
"http://download.finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=sl1d1t1c1ohgv&e=.csv" | |
let mutable _symbol = String.Empty | |
let mutable _current = 0.0 | |
let mutable _open = 0.0 | |
let mutable _high = 0.0 | |
let mutable _low = 0.0 | |
let mutable _volume = 0 | |
do | |
(* We initialize our object in the do block *) | |
let webClient = new WebClient() | |
(* Data comes back as a comma-seperated list, so we split it | |
on each comma *) | |
let data = webClient.DownloadString(url).Split([|','|]) | |
_symbol <- data.[0] | |
_current <- float data.[1] | |
_open <- float data.[5] | |
_high <- float data.[6] | |
_low <- float data.[7] | |
_volume <- int data.[8] | |
member x.Symbol = _symbol | |
member x.Current = _current | |
member x.Open = _open | |
member x.High = _high | |
member x.Low = _low | |
member x.Volume = _volume | |
end | |
let main() = | |
let stocks = | |
["msft"; "noc"; "yhoo"; "gm"] | |
|> Seq.map (fun x -> new Stock(x)) | |
stocks |> Seq.iter (fun x -> printfn "Symbol: %s (%F)" x.Symbol x.Current) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment