Skip to content
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

closures: replace "is called" with "is defined" #2556

Merged
merged 1 commit into from
Feb 12, 2021
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
9 changes: 5 additions & 4 deletions src/ch13-01-closures.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ twice. Unfortunately, we’re now calling this function and waiting for the
result in all cases, which includes the inner `if` block that doesn’t use the
result value at all.

We want to define code in one place in our program, but only *execute* that
code where we actually need the result. This is a use case for closures!
We want to refer to `simulated_expensive_calculation` only once in
`generate_workout`, but defer the expensive calculation to only where
we actually need the result. This is a use case for closures!

#### Refactoring with Closures to Store Code

Expand Down Expand Up @@ -186,8 +187,8 @@ want to use, as shown in Listing 13-6.
<span class="caption">Listing 13-6: Calling the `expensive_closure` we’ve
defined</span>

Now the expensive calculation is called in only one place, and we’re only
executing that code where we need the results.
Now how to perform the expensive calculation is defined in only one
place, and we’re only executing that code where we need the results.

However, we’ve reintroduced one of the problems from Listing 13-3: we’re still
calling the closure twice in the first `if` block, which will call the
Expand Down