Skip to content

Commit

Permalink
Merge pull request #502 from raboof/promises
Browse files Browse the repository at this point in the history
Document non-deprecated way of creating promises
  • Loading branch information
SethTisue committed Mar 22, 2016
2 parents 9aef08c + 9246d84 commit c2c154c
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 97 deletions.
2 changes: 1 addition & 1 deletion es/overviews/core/actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ un future. Estos pueden ser creados utilizando el método `future`. En el siguie
ejemplo, `body` se ejecuta de manera concurrente, retornando un future como
resultado.

val fut = future { body }
val fut = Future { body }
// ...
fut() // wait for future

Expand Down
64 changes: 32 additions & 32 deletions ja/overviews/core/futures.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Future オブジェクトを返すメソッドだということだ。
import ExecutionContext.Implicits.global

val session = socialNetwork.createSessionFor("user", credentials)
val f: Future[List[Friend]] = future {
val f: Future[List[Friend]] = Future {
session.getFriends()
}

Expand All @@ -83,7 +83,7 @@ Future オブジェクトを返すメソッドだということだ。
`NullPointerException` を投げる。この Future `f` は、この例外とともに失敗する:

val session = null
val f: Future[List[Friend]] = future {
val f: Future[List[Friend]] = Future {
session.getFriends
}

Expand All @@ -99,7 +99,7 @@ Future オブジェクトを返すメソッドだということだ。
テキストファイルがあったとして、その中である特定のキーワードが最初に出てきた位置を知りたいとする。
この計算はファイルの内容をディスクから読み込むのにブロッキングする可能性があるため、他の計算と並行実行するのは理にかなっている。

val firstOccurence: Future[Int] = future {
val firstOccurence: Future[Int] = Future {
val source = scala.io.Source.fromFile("myText.txt")
source.toSeq.indexOfSlice("myKeyword")
}
Expand Down Expand Up @@ -129,7 +129,7 @@ Future の実装の多くは、Future の結果を知りたくなったクライ
ソーシャルネットワークの例に戻って、最近の自分の投稿した文のリストを取得して画面に表示したいとする。
これは `List[String]` を返す `getRecentPosts` メソッドを呼ぶことで行われ、戻り値には最近の文のリストが入っている:

val f: Future[List[String]] = future {
val f: Future[List[String]] = Future {
session.getRecentPosts
}

Expand All @@ -141,7 +141,7 @@ Future の実装の多くは、Future の結果を知りたくなったクライ
`onComplete` メソッドは、Future 計算の失敗と成功の両方の結果を扱えるため、汎用性が高い。
成功した結果のみ扱う場合は、(部分関数を受け取る) `onSuccess` コールバックを使う:

val f: Future[List[String]] = future {
val f: Future[List[String]] = Future {
session.getRecentPosts
}

Expand All @@ -151,7 +151,7 @@ Future の実装の多くは、Future の結果を知りたくなったクライ

失敗した結果のみ扱う場合は、`onFailure` コールバックを使う:

val f: Future[List[String]] = future {
val f: Future[List[String]] = Future {
session.getRecentPosts
}

Expand All @@ -168,7 +168,7 @@ Future の実装の多くは、Future の結果を知りたくなったクライ
部分関数は `isDefinedAt` メソッドを持つため、`onFailure` メソッドはコールバックが特定の `Throwable` に対して定義されている場合のみ発火される。
以下の例では、登録された `onFailure` コールバックは発火されない:

val f = future {
val f = Future {
2 / 0
}

Expand All @@ -179,7 +179,7 @@ Future の実装の多くは、Future の結果を知りたくなったクライ

キーワードの初出の位置を検索する例に戻ると、キーワードの位置を画面に表示したいかもしれない:

val firstOccurence: Future[Int] = future {
val firstOccurence: Future[Int] = Future {
val source = scala.io.Source.fromFile("myText.txt")
source.toSeq.indexOfSlice("myKeyword")
}
Expand Down Expand Up @@ -208,7 +208,7 @@ Future 内の値が利用可能となることを必要とするため、Future

@volatile var totalA = 0

val text = future {
val text = Future {
"na" * 16 + "BATMAN!!!"
}

Expand Down Expand Up @@ -253,12 +253,12 @@ Future 内の値が利用可能となることを必要とするため、Future
為替トレードサービスの API があって、米ドルを有利な場合のみ買いたいとする。
まずコールバックを使ってこれを実現してみよう:

val rateQuote = future {
val rateQuote = Future {
connection.getCurrentValue(USD)
}

rateQuote onSuccess { case quote =>
val purchase = future {
val purchase = Future {
if (isProfitable(quote)) connection.buy(amount, quote)
else throw new Exception("有益ではない")
}
Expand Down Expand Up @@ -292,7 +292,7 @@ Future の投射はコレクションの投射と同様に考えることがで

上の例を `map` コンビネータを使って書き換えてみよう:

val rateQuote = future {
val rateQuote = Future {
connection.getCurrentValue(USD)
}

Expand Down Expand Up @@ -329,8 +329,8 @@ Future の設計指針の 1つは for 内包表記から利用できるように
両方の貨幣の為替レートを取得して、両者の値に応じて購入を決定する必要がある。
以下に for 内包表記を使った `flatMap``withFilter` の例をみてみよう:

val usdQuote = future { connection.getCurrentValue(USD) }
val chfQuote = future { connection.getCurrentValue(CHF) }
val usdQuote = Future { connection.getCurrentValue(USD) }
val chfQuote = Future { connection.getCurrentValue(CHF) }

val purchase = for {
usd <- usdQuote
Expand Down Expand Up @@ -405,12 +405,12 @@ Future は同じ `Throwable` とともに失敗する。
この Future と引数の Future が両方失敗した場合は、新しい Future はこの Future の例外とともに失敗する。
以下に米ドルの値を表示することを試みて、米ドルの取得に失敗した場合はスイス・フランの値を表示する具体例をみてみよう:

val usdQuote = future {
val usdQuote = Future {
connection.getCurrentValue(USD)
} map {
usd => "値: " + usd + " USD"
}
val chfQuote = future {
val chfQuote = Future {
connection.getCurrentValue(CHF)
} map {
chf => "値: " + chf + "CHF"
Expand All @@ -429,7 +429,7 @@ Future は同じ `Throwable` とともに失敗する。

val allposts = mutable.Set[String]()

future {
Future {
session.getRecentPosts
} andThen {
posts => allposts ++= posts
Expand All @@ -449,14 +449,14 @@ Future は同じ `Throwable` とともに失敗する。
もし元の Future が成功した場合は、`failed` 投射は `NoSuchElementException`
とともに失敗する。以下は例外を画面に表示する具体例だ:

val f = future {
val f = Future {
2 / 0
}
for (exc <- f.failed) println(exc)

以下の例は画面に何も表示しない:

val f = future {
val f = Future {
4 / 2
}
for (exc <- f.failed) println(exc)
Expand All @@ -474,11 +474,11 @@ the throwable types it matches.
-->

<!--
Invoking the `future` construct uses a global execution context to start an asynchronous computation. In the case the client desires to use a custom execution context to start an asynchronous computation:
Invoking the `Future` construct uses an implicit execution context to start an asynchronous computation. In the case the client desires to use a custom execution context to start an asynchronous computation:
val f = customExecutionContext future {
val f = Future {
4 / 2
}
}(customExecutionContext)
-->

### Future の拡張
Expand All @@ -501,7 +501,7 @@ Future の結果に対してブロックする方法を以下に具体例で説
import scala.concurrent.duration._

def main(args: Array[String]) {
val rateQuote = future {
val rateQuote = Future {
connection.getCurrentValue(USD)
}
Expand Down Expand Up @@ -568,19 +568,19 @@ Promise の `p` は `p.future` によって返される Future を完了させ
ある計算が値を生産し、別の計算がそれを消費する Producer-Consumer の具体例を使って説明しよう。
この値の受け渡しは Promise を使って実現している。

import scala.concurrent.{ future, promise }
import scala.concurrent.{ Future, Promise }
import scala.concurrent.ExecutionContext.Implicits.global

val p = promise[T]
val p = Promise[T]()
val f = p.future

val producer = future {
val producer = Future {
val r = produceSomething()
p success r
continueDoingSomethingUnrelated()
}

val consumer = future {
val consumer = Future {
startDoingSomething()
f onSuccess {
case r => doSomethingWithResult()
Expand All @@ -599,10 +599,10 @@ Promise の `p` は `p.future` によって返される Future を完了させ

以下は Promise を失敗させる具体例だ。

val p = promise[T]
val p = Promise[T]()
val f = p.future

val producer = future {
val producer = Future {
val r = someComputation
if (isInvalid(r))
p failure (new IllegalStateException)
Expand Down Expand Up @@ -639,8 +639,8 @@ HTTP レスポンスにのみ興味がある場合で、これは最初に Promi
渡された Future が完了すると、その Promise も Future の値とともに完了する。
以下のプログラムは `1` と表示する:

val f = future { 1 }
val p = promise[Int]
val f = Future { 1 }
val p = Promise[Int]()

p completeWith f

Expand All @@ -662,7 +662,7 @@ Promise、Future の `onComplete` メソッド、そして `future`
以下のように書くことができる:

def first[T](f: Future[T], g: Future[T]): Future[T] = {
val p = promise[T]
val p = Promise[T]()

f onSuccess {
case x => p.tryComplete(x)
Expand Down
6 changes: 3 additions & 3 deletions overviews/core/_posts/2010-11-30-actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ In addition, a `Future` can be queried to find out whether its result
is available without blocking using the `isSet` method.

A message send-with-future is not the only way to obtain a
future. Futures can also be created from computations using the `future`
method. In the following example, the computation body is started to
future. Futures can also be created from computations directly.
In the following example, the computation body is started to
run concurrently, returning a future for its result:

val fut = future { body }
val fut = Future { body }
// ...
fut() // wait for future

Expand Down
8 changes: 4 additions & 4 deletions overviews/core/_posts/2012-09-20-futures.md
Original file line number Diff line number Diff line change
Expand Up @@ -887,10 +887,10 @@ Consider the following producer-consumer example, in which one computation
produces a value and hands it off to another computation which consumes
that value. This passing of the value is done using a promise.

import scala.concurrent.{ future, promise }
import scala.concurrent.{ Future, Promise }
import scala.concurrent.ExecutionContext.Implicits.global

val p = promise[T]
val p = Promise[T]()
val f = p.future

val producer = Future {
Expand Down Expand Up @@ -922,7 +922,7 @@ promise that has already been completed (or failed) will throw an

The following example shows how to fail a promise.

val p = promise[T]
val p = Promise[T]()
val f = p.future

val producer = Future {
Expand Down Expand Up @@ -971,7 +971,7 @@ future. After the future is completed, the promise gets completed with
the result of that future as well. The following program prints `1`:

val f = Future { 1 }
val p = promise[Int]
val p = Promise[Int]()

p completeWith f

Expand Down
Loading

0 comments on commit c2c154c

Please sign in to comment.