アットウィキロゴ

Example8.5

「Example8.5」の編集履歴(バックアップ)一覧に戻る

Example8.5 - (2011/02/24 (木) 08:44:13) のソース

#co(){
8.5 Tuples

Sometimes, a function needs to return more than one result. For instance, take the function divmod which returns the integer quotient and rest of two given integer arguments. Of course, one can define a class to hold the two results of divmod, as in: 
}


#setmenu2(ex-r-menu)
** 8.5 タプル (Tuples)

関数から1つ以上の結果を返す必要がたまに生じます。たとえば、与えられた2つの整数引数の整数商と余りを返す関数 divmod を考えましょう。もちろん、divmod の2つの結果を保持するクラスを次のようにも定義できます。

 case class TwoInts(first: Int, second: Int) 
 def divmod(x: Int, y: Int): TwoInts = new TwoInts(x / y, x % y)

#co(){
However, having to define a new class for every possible pair of result types is very tedious. In Scala one can use instead a generic class Tuple2, which is defined as follows: 
}

しかし、結果型のペア一つ一つに対し、新しいクラスを定義しなくてはならないのでは、あまりに面倒です。Scala ではその代わりに、次のように定義されたジェネリッククラス Tuple2 を使えます。

 package scala 
 case class Tuple2[A, B](_1: A, _2: B) 

#co(){
With Tuple2, the divmod method can be written as follows. 
}

Tuple2 を用いて divmod メソッドは次のように書けます。

 def divmod(x: Int, y: Int) = new Tuple2[Int, Int](x / y, x % y) 

#co(){
As usual, type parameters to constructors can be omitted if they are deducible from value arguments. There exist also tuple classes for every other number of elements (the current Scala implementation limits this to tuples of some reasonable number of elements). 
}

コンストラクタへの型パラメータは、値引数から推論可能な場合、通常省略できます。異なる要素数ごとにタプルクラスが存在します (現在の Scala 実装では、要素数をある適正値に制限しています) 。

#co(){
How are elements of tuples accessed? Since tuples are case classes, there are two possibilities. One can either access a tuple's fields using the names of the constructor parameters _i , as in the following example: 
}

タプルの要素にどうやってアクセスするのでしょうか? タプルはケースクラスなので、2つの可能性があります。次の例のように、タプルのフィールドにコンストラクタパラメータ名 _i を用いてアクセスできます。

 val xy = divmod(x, y) 
 println("quotient: " + xy._1 + ", rest: " + xy._2) 

#co(){
Or one uses pattern matching on tuples, as in the following example: 
}

あるいは次の例のように、タプルに対するパターンマッチを用います。

 divmod(x, y) match { 
   case Tuple2(n, d) => 
     println("quotient: " + n + ", rest: " + d) 
 } 

#co(){
Note that type parameters are never used in patterns; it would have been illegal to write case Tuple2[Int, Int](n, d). 
}

型パラメータはパターンでは決して使われないことに注意して下さい。「case Tuple2[Int, Int](n, d)」と書くのは文法違反です。(訳注 : たとえば、「case x:Tuple2[Int,Int] =>・・・」と書くのは OK です)

#co(){
Tuples are so convenient that Scala defines special syntax for them. To form a tuple with n elements x1 , . . . , xn one can write (x1 , . . . , xn ). This is equivalent to Tuplen(x1 , . . . , xn ). The (...) syntax works equivalently for types and for patterns. With that tuple syntax, the divmod example is written as follows: 
}

タプルは非常に便利なので、Scala では特別な構文が用意されています。n 要素 x1,...,xn のタプルは、(x1,...,xn) と書けます。これは Tuplen(x1,...,xn) と等価です。(...) 構文は、型やパターンについても同じように働きます。タプル構文を使って、divmod の例は次のように書けます。

 def divmod(x: Int, y: Int): (Int, Int) = (x / y, x % y)

 divmod(x, y) match { 
   case (n, d) => println("quotient: " + n + ", rest: " + d) 
 } 

#center(){[[前ページ>Example8.4]] [[ 8 章>Chapter 8 Generic Types and Methods]] [[目次>ScalaByExample和訳]] [[次ページ>Example8.6]]}

----
#comment
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。