Search code examples
f#seqargument-unpacking

How to convert the x of for x in seq to seq


This is a follow-up question to this SO-post.

Given this block of code (csvData1 is a .csv file.)

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

for x in mappedSeq1 do
    printfn "%A" x

What if I don't want to unpack each element of the sequence into x and a second val we could call y. How can I convert x to a sequence, so I can use Seq.fst and Seq.snd?

I know it is possible to unpack these elements. My question is about finding an alternate way to do this, especially given that x is a System.Tuple.


Solution

  • x is a tuple of two strings (which is what I assume csvRow.[i] are), because that's how you yield it from the sequence. And you can destructure it right in your for loop:

    for (x,y) in mappedSeq1 do
       printfn "%s - %s" x y
    

    or you can use fst x or snd x if you do not want to destructure x.