Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# creating smaller tuple using Seq.map

Tags:

f#

I have written this F# code:

let tuples = [|("A",1,0);("A",2,3);("A",3,6)|]
let tupleSubset =
tuples
|> Seq.map(fun values -> 
    values.[0],
    values.[2])
printfn "%A" tupleSubset

I am getting: The operator 'expr.[idx]' has been used on an object of indeterminate type based on information prior to this program point. Consider adding further type constraints

Can anyone tell me what I am doing wrong?

like image 837
Jamie Dixon Avatar asked Mar 23 '23 18:03

Jamie Dixon


1 Answers

A simple way around the error you are getting would be the following:

let tuples = [|("A",1,0); ("A",2,3); ("A",3,6)|]
let tupleSubset = tuples |> Seq.map (fun (a, b, c) -> a, b)
printfn "%A" tupleSubset

As to why you are getting the error: note that the kind of index dereferencing you are attempting with values.[0] and values.[1] works for arrays, dictionaries, etc., but not for tuples, whereas each values has the type string * int * int.

Since you don't need the third element of the tuple, you can even choose not to bind it to a symbol, by writing the second line as follows:

let tupleSubset = tuples |> Seq.map (fun (a, b, _) -> a, b)
like image 154
Shredderroy Avatar answered Mar 26 '23 08:03

Shredderroy