-1

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.

Community
  • 1
  • 1
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
  • *"what is `x`?"* Are you using a development tool with type information? For example, in Visual Studio, you can see the type of `x` by hovering your mouse over it. – Mark Seemann Sep 14 '16 at 17:51
  • Yes, x is a System.Tuple, but I cannot structure the code to use Seq.fst and so on. – octopusgrabbus Sep 14 '16 at 17:53
  • I've removed the What is x portion of the OP. Basically, I want to pull the first or second element from the System.Tuple without unpacking. It's more or less an academic request. I just want to know if there's another way. – octopusgrabbus Sep 14 '16 at 17:55
  • 1
    You can use `fst` and `snd` instead of destructuring... – Mark Seemann Sep 14 '16 at 18:00
  • Call `fst x` or `snd x`. A tuple is not a `seq`, and `fst`/`snd` come from the `Operators` module. There's no `Seq.fst`, at least not one that does what you want. – scrwtp Sep 14 '16 at 18:03
  • That's it. I'm confusing the sequence with a tuple that was derived from it. Thanks. – octopusgrabbus Sep 14 '16 at 18:07

2 Answers2

4

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.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
  • I don't want to destructure x. For lack of a better way to describe it, I'd like to cast x to a Seq, so I can use Seq's methods. – octopusgrabbus Sep 14 '16 at 17:52
  • 1
    I don't quite understand how you want to "cast x to Seq". `x` is a tuple, not a sequence, so you can't "cast it" to one. Did you mean that you wanted to _create_ a sequence with the two elements in it? Then you can just make a list `[x; y]`. Did you mean something else? – Fyodor Soikin Sep 14 '16 at 19:22
0

Your code currently creates a sequence which reads rows and produces tuples of columns 2 and 5.

If you want to return a sequence of Row0Col2; Row0Col5; Row1Col2; Row1Col5 (the two columns will need to be of the same type), then use

yield! [ csvRow.[2], csvRow.[5] ]

Or if you want a sequence of sequences - [Row0Col2; Row0Col5]; [Row1Col2; Ro1Col5], then use

yield [ csvRow.[2]; csvRow.[5] ]

yield! will yield each individual item from the lists, whereas yield will just yield the lists in sequence.

marklam
  • 5,346
  • 1
  • 24
  • 26