Question

I'm an F# newbie and am having issues with my the code I am writing, for some reason the row split does not work and only a null array is returned by the Map.OfArray call.

code below:

let loaddata() =
       async {
            let! csv = sprintf "http://api.bitcoincharts.com/v1/trades.csv?symbol=bitstampUSD" |> fetch
            return
                [|
                    for row in csv.Split([|'\n'|], StringSplitOptions.RemoveEmptyEntries) do
                        match row.Split([|','|]) with
                        | [|d; p; v |]-> ()
                        | _ -> ()
                |] |> Map.ofArray
            }

This is my fetch command:

let fetch (url:string) =
    async {
            let wp = new WebProxy()
            let request = System.Net.WebRequest.Create(url)
            let! resp = request.AsyncGetResponse()
            let ms= new MemoryStream()
            resp.GetResponseStream().CopyTo(ms)
            return (System.Text.Encoding.UTF8.GetString(ms.ToArray()))
          }
Was it helpful?

Solution

You need to yield the matched values from your inner sequence comprehension. It's not clear what the format of the keys and values should be but you can do something like:

return
    [|
        for row in csv.Split([|'\n'|], StringSplitOptions.RemoveEmptyEntries) do
            match row.Split([|','|]) with
            | [|d; p; v |]-> yield (d, p)
            | _ -> yield! [||]
    |] |> Map.ofArray
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top