What about tuple accessor function for (,,) ... ?

There is a fst and a snd function. What about fstOf3 ( \(x,_,_) -> x ) sndOf3 ( \(_,x,_) -> x ) thrdOf3 ( \(_,_,x) -> x ) ? ... or using overloading ? fst snd thrd _4th _5th I think its easier to type/ read than \(_,_,x,_) -> x class Second a b | a -> b where t2 :: a -> b instance Second (a,b) b where t2 (a,b) = b instance Second (a,b,c) b where t2 (a,b,c) = b instance Second (a,b,c,d) b where t2 (a,b,c,d) = b instance Second (a,b,c,d,e) b where t2 (a,b,c,d,e) = b Or is there any drawback I didn't see? I'd like to see them in Data.Tuple Marc

Hi Marc,
What about fstOf3 ( \(x,_,_) -> x ) sndOf3 ( \(_,x,_) -> x ) thrdOf3 ( \(_,_,x) -> x )
I have defined "fst3", "snd3", "thd3". I would strongly support adding them to the standard libraries, in Data.Tuple.
class Second a b | a -> b where t2 :: a -> b instance Second (a,b) b where t2 (a,b) = b instance Second (a,b,c) b where t2 (a,b,c) = b instance Second (a,b,c,d) b where t2 (a,b,c,d) = b instance Second (a,b,c,d,e) b where t2 (a,b,c,d,e) = b
I do not think we should encourage this, if people want to do these kind of tuple tricks, the solution is one of: 1) make tuples inductive (a,(b,c)), then thd = snd . snd 2) use HList (i think this would give you want you need) 3) use records The Yhc compiler uses 8 element tuples in some places - its not a good solution ever. I suspect the use of the 2 element tuple is really common, the 3 element tuple sees a few outings, and the 4 and above element tuple is a bit of a rarity. Following the proposal tradition, there about about 100 hits for each of fst3 etc on Google Code Search. They occur in HaXml, Cabal, Gofer, Hat, Chameleon, Hoogle, nhc, Yhc etc. Thanks Neil

Hi Neil.
The Yhc compiler uses 8 element tuples in some places - its not a good solution ever. Using them I would be counting commas all the time ;)
I suspect the use of the 2 element tuple is really common, the 3 element tuple sees a few outings, and the 4 and above element tuple is a bit of a rarity.
Thanks for your answer. This 4 tuple design can be found in regex-base (Text.Regex.Base.Context) (before, match, after, [submatch]) I only did need the submatches. tuple -> hlist -> get 3 element would be an option too ;) But you are right. It would be mutch better to use (BMAS = Before Match After Submatches) data BMASContext = BMASContext { before:: , match:: , after::, submatches :: .. } Then I could do submatches ( "..." =~ "..." ) which would be a lot more readable, too. You can define your own. I have to dive into the regex code much deeper.. It should have been a quick hack :) Marc

Neil Mitchell wrote:
I suspect the use of the 2 element tuple is really common, the 3 element tuple sees a few outings, and the 4 and above element tuple is a bit of a rarity.
Right. I suspect the biggest use of the longer tuples is beginners doing this: data MyType = MyType (Int,Int,Char,Bool,Int) instead of this: data MyType = MyType Int Int Char Bool Int -- Ashley Yakeley
participants (3)
-
Ashley Yakeley
-
Marc Weber
-
Neil Mitchell