Skip to content

Commit

Permalink
codelab: Update Factor lab to match instructions
Browse files Browse the repository at this point in the history
Uses absl::optional<int> rather than returning a bool and setting an
int* arg

Change-Id: I7ff642eeb221fe9124ad7018e5aa5d8de3361ec4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3511035
Auto-Submit: Austin Sullivan <[email protected]>
Reviewed-by: Ayu Ishii <[email protected]>
Commit-Queue: Ayu Ishii <[email protected]>
Cr-Commit-Position: refs/heads/main@{#979287}
  • Loading branch information
a-sully authored and Chromium LUCI CQ committed Mar 9, 2022
1 parent 7dc8681 commit 131cf84
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
17 changes: 8 additions & 9 deletions codelabs/cpp101/codelab.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,14 @@ or `base::PlatformThread::Sleep`).

Take the given (slow) function to find a non-trivial factor of a given integer:
```cpp
bool FindNonTrivialFactor(int n, int* factor) {
  // Really naive algorithm.
  for (int i = n-1; i >= 2; --i) {
   if (n % i == 0) {
     *factor = i;
      return true;
    }
  }
  return false;
absl::optional<int> FindNonTrivialFactor(int n) {
// Really naive algorithm.
for (int i = 2; i < n; ++i) {
if (n % i == 0) {
return i;
}
}
return absl::nullopt;
}
```
Write a command-line utility `factor` that takes a number, posts a task to the
Expand Down
4 changes: 2 additions & 2 deletions codelabs/cpp101/factor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ int main(int argc, char* argv[]) {

absl::optional<int> result;
// Notice that we're posting the long-running factoring operation to
// |base::ThreadPool| to avoid blocking the main thread
// `base::ThreadPool` to avoid blocking the main thread.
//
// |run_loop.QuitClosure()| will be called once the factoring task completes.
// `run_loop.QuitClosure()` will be called once the factoring task completes.
base::ThreadPool::PostTaskAndReply(
FROM_HERE, base::BindOnce(&FindNonTrivialFactorHelper, n, &result),
run_loop.QuitClosure());
Expand Down

0 comments on commit 131cf84

Please sign in to comment.