Example5.1

「Example5.1」の編集履歴(バックアップ)一覧はこちら

Example5.1 - (2011/02/24 (木) 08:33:42) の1つ前との変更点

追加された行は緑色になります。

削除された行は赤色になります。

#co(){ 5.1 Anonymous Functions Parameterization by functions tends to create many small functions. In the previous example, we defined id, square and power as separate functions, so that they could be passed as arguments to sum. Instead of using named function definitions for these small argument functions, we can formulate them in a shorter way as anonymous functions. An anonymous function is an expression that evaluates to a function; the function is defined without giving it a name. As an example consider the anonymous square function: } ** 5.1 無名関数 (Anonymous Functions) 関数を引数とすることで、多くの小さな関数が作られます。前の例では id, square, powerOfTwo をそれぞれ別個の関数として定義し、sum に引数として渡せました。 引数用の小さな関数のために名前付きの関数定義を使う代わりに、それらを無名関数として短く書けます。&bold(){無名関数}は関数に評価される式で、関数に名前を付けずに定義できます。例として、2乗する無名関数を考えます。 (x: Int) => x * x #co(){ The part before the arrow '=>' are the parameters of the function, whereas the part following the '=>' is its body. For instance, here is an anonymous function which multiples its two arguments. } 矢印 '=>' の前の部分は関数の引数で、'=>' に続く部分が本体です。たとえば、2つの引数を掛け合わせる無名関数は次のようになります。 (x: Int, y: Int) => x * y #co(){ Using anonymous functions, we can reformulate the first two summation functions without named auxiliary functions: } 無名関数により名前付きの外部関数を使わずに、先ほどの最初の2つの、和を求める関数を書けます。 def sumInts(a: Int, b: Int): Int = sum((x: Int) => x, a, b) def sumSquares(a: Int, b: Int): Int = sum((x: Int) => x * x, a, b) #co(){ Often, the Scala compiler can deduce the parameter type(s) from the context of the anonymous function in which case they can be omitted. For instance, in the case of sumInts or sumSquares, one knows from the type of sum that the first parameter must be a function of type Int => Int. Hence, the parameter type Int is redundant and may be omitted. If there is a single parameter without a type, we may also omit the parentheses around it: } しばしば、Scala コンパイラは無名関数の文脈からその引数の型を推論でき、その場合は引数の型を省略できます。たとえば sumInts や sumSquares では、sum の型から最初の引数は Int => Int 型の関数に違いないと分かります。ですから、引数の型 Int は冗長であり省略できます。もし、型指定のない、ただ1つの引数しかない場合は、その周りの括弧も省略できます。 def sumInts(a: Int, b: Int): Int = sum(x => x, a, b) def sumSquares(a: Int, b: Int): Int = sum(x => x * x, a, b) #co(){ Generally, the Scala term (x1 : T1 , ..., xn : Tn ) => E defines a function which maps its parameters x1 , ..., xn to the result of the expression E (where E may refer to x1 , ..., xn ). Anonymous functions are not essential language elements of Scala, as they can always be expressed in terms of named functions. Indeed, the anonymous function } 一般的に Scala の項 (x1:T1, ..., xn:Tn) => E は、引数 x1, ..., xn を式 E (E は x1, ..., xn を参照しているでしょう) の結果に対応させる関数を定義します。無名関数は Scala に必須の構文ではありません。なぜなら常に名前付き関数で表現できるからです。実際、無名関数 (x1 : T1, ..., xn : Tn ) => E #co(){ is equivalent to the block } はブロック { def f (x1 : T1, ..., xn : Tn ) = E ; f _ } #co(){ where f is fresh name which is used nowhere else in the program. We also say, anonymous functions are "syntactic sugar". } と等価です。ただし f はプログラムのどこか他で使われていない新規な名前です。無名関数は「&bold(){糖衣構文}」であるとも言います。 #center(){[[前ページ>ExampleChap5]] [[ 5 章>ExampleChap5]] [[目次>ScalaByExample和訳]] [[次ページ>Example5.2]]} ---- - 本当にお疲れさまです (実に素晴らしい貢献ですね(^^)) 。「それら引数の関数の為」の「引数の関数」は「引数のための (小さな) 関数」もしくは「引数用の (小さな) 関数」の方が良いように感じました。 -- ryugate (2008-05-19 22:27:45) - 「しばしば Scala コンパイラは省略可能な場合には仮引数型を無名関数の文脈から推論可能です。」は、直訳的ですが「しばしば、Scala コンパイラは無名関数の文脈からそのパラメータの型を推論でき、その場合はそれらは省略できます。」な感じはいかがでしょうか。 -- ryugate (2008-05-19 22:36:32) #comment
#co(){ 5.1 Anonymous Functions Parameterization by functions tends to create many small functions. In the previous example, we defined id, square and power as separate functions, so that they could be passed as arguments to sum. Instead of using named function definitions for these small argument functions, we can formulate them in a shorter way as anonymous functions. An anonymous function is an expression that evaluates to a function; the function is defined without giving it a name. As an example consider the anonymous square function: } #setmenu2(ex-r-menu) ** 5.1 無名関数 (Anonymous Functions) 関数をパラメータとすることで、多くの小さな関数が作られます。前の例では id, square, powerOfTwo をそれぞれ別個の関数として定義し、sum に引数として渡せました。 引数用の小さな関数のために名前付きの関数定義を使う代わりに、それらを無名関数として短く書けます。&bold(){無名関数}は関数に評価される式で、関数に名前を付けずに定義できます。例として、2乗する無名関数を考えます。 (x: Int) => x * x #co(){ The part before the arrow '=>' are the parameters of the function, whereas the part following the '=>' is its body. For instance, here is an anonymous function which multiples its two arguments. } 矢印 '=>' の前の部分は関数のパラメータで、'=>' に続く部分が本体です。たとえば、2つの引数を掛け合わせる無名関数は次のようになります。 (x: Int, y: Int) => x * y #co(){ Using anonymous functions, we can reformulate the first two summation functions without named auxiliary functions: } 無名関数により名前付きの外部関数を使わずに、先ほどの最初の2つの、和を求める関数を書けます。 def sumInts(a: Int, b: Int): Int = sum((x: Int) => x, a, b) def sumSquares(a: Int, b: Int): Int = sum((x: Int) => x * x, a, b) #co(){ Often, the Scala compiler can deduce the parameter type(s) from the context of the anonymous function in which case they can be omitted. For instance, in the case of sumInts or sumSquares, one knows from the type of sum that the first parameter must be a function of type Int => Int. Hence, the parameter type Int is redundant and may be omitted. If there is a single parameter without a type, we may also omit the parentheses around it: } しばしば、Scala コンパイラは無名関数の文脈からそのパラメータの型を推論でき、その場合はパラメータの型を省略できます。たとえば sumInts や sumSquares では、sum の型から最初のパラメータは Int => Int 型の関数に違いないと分かります。ですから、パラメータの型 Int は冗長であり省略できます。もし、型指定のない、ただ1つのパラメータしかない場合は、その周りの括弧も省略できます。 def sumInts(a: Int, b: Int): Int = sum(x => x, a, b) def sumSquares(a: Int, b: Int): Int = sum(x => x * x, a, b) #co(){ Generally, the Scala term (x1 : T1 , ..., xn : Tn ) => E defines a function which maps its parameters x1 , ..., xn to the result of the expression E (where E may refer to x1 , ..., xn ). Anonymous functions are not essential language elements of Scala, as they can always be expressed in terms of named functions. Indeed, the anonymous function } 一般的に Scala の項 (x1:T1, ..., xn:Tn) => E は、パラメータ x1, ..., xn を式 E (E は x1, ..., xn を参照しているでしょう) の結果に対応させる関数を定義します。無名関数は Scala に必須の構文ではありません。なぜなら常に名前付き関数で表現できるからです。実際、無名関数 (x1 : T1, ..., xn : Tn ) => E #co(){ is equivalent to the block } はブロック { def f (x1 : T1, ..., xn : Tn ) = E ; f _ } #co(){ where f is fresh name which is used nowhere else in the program. We also say, anonymous functions are "syntactic sugar". } と等価です。ただし f はプログラムのどこか他で使われていない新規な名前です。無名関数は「糖衣構文」であるとも言います。 #center(){[[前ページ>ExampleChap5]] [[ 5 章>ExampleChap5]] [[目次>ScalaByExample和訳]] [[次ページ>Example5.2]]} ---- - 本当にお疲れさまです (実に素晴らしい貢献ですね(^^)) 。「それら引数の関数の為」の「引数の関数」は「引数のための (小さな) 関数」もしくは「引数用の (小さな) 関数」の方が良いように感じました。 -- ryugate (2008-05-19 22:27:45) - 「しばしば Scala コンパイラは省略可能な場合には仮引数型を無名関数の文脈から推論可能です。」は、直訳的ですが「しばしば、Scala コンパイラは無名関数の文脈からそのパラメータの型を推論でき、その場合はそれらは省略できます。」な感じはいかがでしょうか。 -- ryugate (2008-05-19 22:36:32) #comment

表示オプション

横に並べて表示:
変化行の前後のみ表示:
ツールボックス

下から選んでください:

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