문제

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()))
          }
도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top