okay, this might be a silly question.
So I have some tuples of size 4 so like (int,int,int,int)
If it were a 2 tuple I could use fst(myTuple) to refer to the first element. How could I, say, refer to the third element of a 4 tuple?
okay, this might be a silly question.
So I have some tuples of size 4 so like (int,int,int,int)
If it were a 2 tuple I could use fst(myTuple) to refer to the first element. How could I, say, refer to the third element of a 4 tuple?
If you want random access to a generally sized tuple, then it is not possible. For any given size, you can follow ildjarn's answer (extending it for four, five, etc.), but that it the only (functional) way.
A possibility for tuples in general, is to convert it to a list first, as found here, but that's not too pretty as it requires reflection.
For the sheer novelty, here's an overloaded operator that works for tuples of any* size.
* Any, in this context, has a more limited meaning, specifically, up to 7.
Use pattern matching:
This is described directly in the MSDN documentation for tuples: Tuples (F#)
Here's a version of @Daniels novel solution which calculates
Rest
offsets of the underlying Tuple representation to support position-based access on arbitrarily long tuples. Error handling omitted.