Skip to content

translate tour/automatic-closures into Japanese #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions _ja/tour/automatic-closures.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
layout: tour
title: Automatic Type-Dependent Closure Construction
title: 型依存クロージャの自動構築
language: ja

discourse: true

partof: scala-tour
---

Scala allows parameterless function names as parameters of methods. When such a method is called, the actual parameters for parameterless function names are not evaluated and a nullary function is passed instead which encapsulates the computation of the corresponding parameter (so-called *call-by-name* evaluation).
Scalaはメソッドのパラメータにパラメータを受け取らない関数名が使えます。このようなメソッドが呼び出される時、パラメータを受け取らない関数名は実際に評価されず、代わりに対応するパラメーターの処理をカプセル化した、引数を取らない関数が渡されます(そのため *名前渡し*評価と呼ばれます)。

The following code demonstrates this mechanism:
以下のコードはこの仕組みを説明しています。

object TargetTest1 extends Application {
def whileLoop(cond: => Boolean)(body: => Unit): Unit =
Expand All @@ -25,11 +25,11 @@ The following code demonstrates this mechanism:
}
}

The function whileLoop takes two parameters `cond` and `body`. When the function is applied, the actual parameters do not get evaluated. But whenever the formal parameters are used in the body of `whileLoop`, the implicitly created nullary functions will be evaluated instead. Thus, our method `whileLoop` implements a Java-like while-loop with a recursive implementation scheme.
関数 whileLoop は2つのパラメータ`cond``body`を受け取ります。関数が適用される時、実際のパラメータは評価されません。しかし形式上のパラメータが`whileLoop`の本体内で使われる度に、暗黙に生成された引数の無い関数が代わりに評価されます。このようにメソッド`whileLoop`はJavaのようなwhileループを再帰的な実装案で実装しています。

We can combine the use of [infix/postfix operators](operators.html) with this mechanism to create more complex statements (with a nice syntax).
[中置/後置 演算子](operators.html)とこのメカニズムを組み合わせ利用し、より複雑な式を(より良い構文で)が作れます。

Here is the implementation of a loop-unless statement:
こちらがloop-unless式の実装です。

object TargetTest2 extends Application {
def loop(body: => Unit): LoopUnlessCond =
Expand All @@ -46,9 +46,9 @@ Here is the implementation of a loop-unless statement:
i -= 1
} unless (i == 0)
}
The `loop` function just accepts a body of a loop and returns an instance of class `LoopUnlessCond` (which encapsulates this body object). Note that the body didn't get evaluated yet. Class `LoopUnlessCond` has a method `unless` which we can use as a *infix operator*. This way, we achieve a quite natural syntax for our new loop: `loop { < stats > } unless ( < cond > )`.
この`loop`関数はループ処理の本体を受け取り、クラス`LoopUnlessCond`(bodyのオブジェクトをカプセル化する)のインスタンスを返すだけです。bodyはまだ評価されていないことに気をつけてください。クラス`LoopUnlessCond`は中置演算子として使えるメソッド`unless`を持ちます。このようにより自然な構文で新しいループ処理`loop { < stats > } unless ( < cond > )`を作れます。

Here's the output when `TargetTest2` gets executed:
こちらが`TargetTest2`を実行した時の出力です。

i = 10
i = 9
Expand Down