Skip to content

Instantly share code, notes, and snippets.

@dmalikov
Created September 24, 2013 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmalikov/6691197 to your computer and use it in GitHub Desktop.
Save dmalikov/6691197 to your computer and use it in GitHub Desktop.
nice constructor syntax
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