SG

組の限界

 ふと、組(タプル)は、いくつまでの要素を含むことができるんだろう、と疑問が浮かんだので実験してみることにする。恐らく、Action や Func のように、16個までの型パラメータが用意された Tuple がいるんだろうなーと予想。つまり、0 から F までです。タプルがFになる

境界を調査する

 しょうがねぇな。組の要素の数をスカウターで計測してやるよ 16個まであるはずだ…… ポチッ


> // スカウター
let t16 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
let t17 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
let t30 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29);;

val t16 :
int * int * int * int * int * int * int * int * int * int * int * int * int *
int * int * int = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
val t17 :
int * int * int * int * int * int * int * int * int * int * int * int * int *
int * int * int * int =
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
val t30 :
int * int * int * int * int * int * int * int * int * int * int * int * int *
int * int * int * int * int * int * int * int * int * int * int * int * int *
int * int * int * int =
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29)

 ……16……17……30……バ……バカな!まだあがっていく!?

リストから組に変換する

 多くの要素を記述するのが面倒になってきたので、リストを組に変換して実験してみます。ひょっとして組にもリストのような記法があるのでしょうか。


> // リストからタプルへ
let genericToObj source = List.map (fun x -> x :> obj) source
let listToTuple l =
let l' = List.toArray l
let types = l'
|> Array.map (fun o -> o.GetType())
let tupleType = Microsoft.FSharp.Reflection.FSharpType.MakeTupleType types
Microsoft.FSharp.Reflection.FSharpValue.MakeTuple (l', tupleType)

let t = listToTuple(genericToObj([1 .. 100]))

let ty = t.GetType();;

val genericToObj : 'a list -> obj list
val listToTuple : obj list -> obj
val t : obj =
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99, 100)
val ty : System.Type =
System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`8[System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Tuple`2[System.Int32,System.Int32]]]]]]]]]]]]]]]

 変換できるようです。特別限界は決められていないっぽい?

MSDNマガジン

 Google 先生に質問してみたところ、解は MSDN マガジンの記事にありました。
 CLR 徹底解剖 - タプルを構築する
 8 を超える要素を持つ組が生成されるときはコンパイラが Tuple.Create メソッドを呼び出しているようです。なるほどね。結構前に判明していたことだったのですね。
 というわけで、要素を持てる数は気にしなくてもよさそうです。


 ※リストをタプルに変換する方法はこちらを使用させていただきました。