8000 updated syntax from "future{ ... }" (deprecated) to "Future{ ... }" in the examples by seralf · Pull Request #412 · scala/docs.scala-lang · GitHub
[go: up one dir, main page]

Skip to content

updated syntax from "future{ ... }" (deprecated) to "Future{ ... }" in the examples #412

8000
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 1 commit into from
Jun 19, 2015
Merged
Changes from all commits
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
52 changes: 26 additions & 26 deletions overviews/core/_posts/2012-09-20-futures.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ a request to obtain a list of friends of a particular user:
import ExecutionContext.Implicits.global

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

Above, we first import the contents of the `scala.concurrent` package
to make the type `Future` and the construct `future` visible.
to make the type `Future` and the construct `Future` visible.
We will explain the second import shortly.

We then initialize a session variable which we will use to send
Expand All @@ -104,7 +104,7 @@ initialized, so the computation in the `future` block will throw a `NullPointerE
This future `f` is then failed with this exception instead of being completed successfully:

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

Expand All @@ -128,7 +128,7 @@ This computation may involve blocking while the file contents
are being retrieved from the disk, so it makes sense to perform it
concurrently with the rest of the computation.

val firstOccurrence: Future[Int] = future {
val firstOccurrence: Future[Int] = Future {
val source = scala.io.Source.fromFile("myText.txt")
source.toSeq.indexOfSlice("myKeyword")
}
Expand Down Expand Up @@ -179,7 +179,7 @@ a `List[String]`-- a list of recent textual posts:

import scala.util.{Success, Failure}

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

Expand All @@ -193,7 +193,7 @@ client to handle the result of both failed and successful future
computations. To handle only successful results, the `onSuccess`
callback is used (which takes a partial function):

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

Expand All @@ -203,7 +203,7 @@ callback is used (which takes a partial function):

To handle failed results, the `onFailure` callback is used:

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

Expand All @@ -223,7 +223,7 @@ Since partial functions have the `isDefinedAt` method, the
particular `Throwable`. In the following example the registered `onFailure`
callback is never triggered:

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

Expand All @@ -236,7 +236,7 @@ Coming back to the previous example with searching for the first
occurrence of a keyword, you might want to print the position
of the keyword to the screen:

val firstOccurrence: Future[Int] = future {
val firstOccurrence: Future[Int] = Future {
val source = scala.io.Source.fromFile("myText.txt")
source.toSeq.indexOfSlice("myKeyword")
}
Expand Down Expand Up @@ -275,7 +275,7 @@ text.

@volatile var totalA = 0

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

Expand Down Expand Up @@ -336,12 +336,12 @@ interfacing with a currency trading service. Suppose we want to buy US
dollars, but only when it's profitable. We first show how this could
be done using callbacks:

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("not profitable")
}
Expand Down Expand Up @@ -384,7 +384,7 @@ about mapping collections.

Let's rewrite the previous example using the `map` combinator:

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

Expand Down Expand Up @@ -429,8 +429,8 @@ Lets assume that we want to exchange US dollars for Swiss francs
buying based on both quotes.
Here is an example of `flatMap` and `withFilter` usage within for-comprehensions:

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 @@ -527,12 +527,12 @@ the exception from this future, as in the following example which
tries to print US dollar value, but prints the Swiss franc value in
the case it fails to obtain the dollar value:

val usdQuote = future {
val usdQuote = Future {
connection.getCurrentValue(USD)
} map {
usd => "Value: " + usd + "$"
}
val chfQuote = future {
val chfQuote = Future {
connection.getCurrentValue(CHF)
} map {
chf => "Value: " + chf + "CHF"
Expand All @@ -554,7 +554,7 @@ and then renders all the posts to the screen:

val allposts = mutable.Set[String]()

future {
Future {
session.getRecentPosts
} andThen {
posts => allposts ++= posts
Expand All @@ -578,14 +578,14 @@ futures also have projections. If the original future fails, the
fails with a `NoSuchElementException`. The following is an example
which prints the exception to the screen:

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

The following example does not print anything to the screen:

val f = future {
val f = Future {
4 / 2
}
for (exc <- f.failed) println(exc)
Expand All @@ -605,7 +605,7 @@ 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:

val f = customExecutionContext future {
val f = customExecutionContext Future {
4 / 2
}
-->
Expand All @@ -631,7 +631,7 @@ Here is an example of how to block on the result of a future:
import scala.concurrent.duration._

def main(args: Array[String]) {
val rateQuote = future {
val rateQuote = Future {
connection.getCurrentValue(USD)
}

Expand Down Expand Up @@ -724,13 +724,13 @@ that value. This passing of the value is done using a promise.
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 @@ -756,7 +756,7 @@ The following example shows how to fail a promise.
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 @@ -801,7 +801,7 @@ The method `completeWith` completes the promise with another
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 f = Future { 1 }
val p = promise[Int]

p completeWith f
Expand Down
0