How to extract data from iterating a sequence

前提是你 提交于 2020-01-05 06:15:24

问题


csvData1 contains the data in a .csv file. I've created a sequence out of just two of the columns in the spreadsheet ("GIC-ID", "COVERAGE DESCRIPTION")

let mappedSeq1 = 
   seq { for csvRow in csvData1 do yield (csvRow.[2], csvRow.[5]) }

Looking in the Visual Studio debugger x winds up being a System.Tuple<string,string>.

for x in mappedSeq1 do
    printfn "%A" x
    printfn "%A" x.ToString

Here is the result of executing

for x in mappedSeq1

("GIC-ID", "COVERAGE DESCRIPTION")
<fun:main@28>

I am having difficulty figuring out how to access x, so I can extract the first element.


回答1:


You can use pattern matching to deconstruct the tuple

for (a, b) in mappedSeq1 do
  // ...

or

for x in mappedSeq1 do
  let a, b = x

Alternatively for a 2-tuple you can use the built-in function fst and snd




回答2:


Use Seq.map and fst to get a sequence of only the first component of a tuple:

let firstOnly = mappedSeq |> Seq.map fst


来源:https://stackoverflow.com/questions/39477638/how-to-extract-data-from-iterating-a-sequence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!