Skip to content

Commit

Permalink
Merge pull request #2556 from marc-h38/call-vs-define
Browse files Browse the repository at this point in the history
closures: replace "is called" with "is defined"
  • Loading branch information
carols10cents authored Feb 12, 2021
2 parents b7ab0f0 + 8acef6c commit a2168e1
Showing 1 changed file with 5 additions and 4 deletions.
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

0 comments on commit a2168e1

Please sign in to comment.