user3623025
user3623025

Reputation: 101

F# Map.ofArray issue with my code

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()))
          }

Upvotes: 1

Views: 271

Answers (1)

Lee
Lee

Reputation: 144136

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

Upvotes: 2

Related Questions