F# and Cool Findings

by Lord Cod3n 31. March 2009 03:42

So I found the video below and began playing with some of the code in it.

http://channel9.msdn.com/pdc2008/TL11/

I am very impressed with F# and planning to see how I can work it into my tool box.  Here is some sample code that goes to Yahoo, pulls down a csv, and parses it.  The syntax, once you are familiar with it, is very easy to read.  So much work with so little code, in my opinion, and it does a lot,  You tell the complier what you want, not what to do.  Here is the code:

#light
open  System.Net
open System.IO
let loadPrices ticker =
    let url = "http://ichart.finance.yahoo.com/table.csv?s=" + ticker + "&d=2&e=31&f=2009&g=d&a=2&b=13&c=1986&ignore=.csv"
    let req = WebRequest.Create(url)
    let resp = req.GetResponse()
    let stream = resp.GetResponseStream()
    let reader = new StreamReader(stream)
    let csv = reader.ReadToEnd()

    let prices =
        csv.Split([| '\n' |] )
        |> Seq.skip 1
        |> Seq.map(fun line -> line.Split([| ',' |]))
        |> Seq.filter(fun values -> values |> Seq.length=7)
        |> Seq.map(fun values ->
            System.DateTime.Parse(values.[0]), float values.[6])
    prices
open System.Windows.Forms
let grid(prices:seq<System.DateTime * float>)=
    let form = new Form(Visible = true,TopMost = true)
    let grid = new DataGridView(Dock = DockStyle.Fill,Visible = true)
    form.Controls.Add(grid)
    grid.DataSource <- prices |> Seq.to_array

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

William Moore is the lead Software Architect and Technologist for Coden Enterprises. He has more than a decade of software development experience primarily Microsoft Platforms.  William enjoys the full gamit of coding everything from the UI down to the database.

Most comments

Page List