-
Notifications
You must be signed in to change notification settings - Fork 274
Add a lazyt class for delaying computations #5090
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Util | ||
|
||
Author: Romain Brenguier, [email protected] | ||
|
||
\*******************************************************************/ | ||
|
||
#ifndef CPROVER_UTIL_LAZY_H | ||
#define CPROVER_UTIL_LAZY_H | ||
|
||
#include <functional> | ||
#include <util/optional.h> | ||
|
||
template <typename valuet> | ||
class lazyt | ||
{ | ||
public: | ||
/// Delay the computation of \p fun to the next time the \c force method | ||
/// is called. | ||
static lazyt from_fun(std::function<valuet()> fun) | ||
{ | ||
return lazyt{std::move(fun)}; | ||
} | ||
|
||
/// Force the computation of the value. If it was already computed, | ||
/// return the same result. | ||
valuet force() | ||
{ | ||
if(value) | ||
return *value; | ||
value = evaluation_function(); | ||
return *value; | ||
} | ||
|
||
private: | ||
optionalt<valuet> value; | ||
std::function<valuet()> evaluation_function; | ||
|
||
explicit lazyt(std::function<valuet()> fun) | ||
: evaluation_function(std::move(fun)) | ||
{ | ||
} | ||
}; | ||
|
||
#endif // CPROVER_UTIL_LAZY_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Unit tests for lazy | ||
|
||
Author: Romain Brenguier, [email protected] | ||
|
||
\*******************************************************************/ | ||
|
||
#include <testing-utils/use_catch.h> | ||
#include <util/lazy.h> | ||
|
||
SCENARIO("lazy test", "[core][util][lazy]") | ||
{ | ||
std::size_t call_counter = 0; | ||
auto length_with_counter = [&call_counter](const std::string &s) { | ||
++call_counter; | ||
return s.length(); | ||
}; | ||
auto lazy_length = | ||
lazyt<int>::from_fun([&]() { return length_with_counter("foo"); }); | ||
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😊 https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/lazy-expressions
Also, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm adding it in a follow-up PR: #5101 |
||
|
||
REQUIRE(call_counter == 0); | ||
auto result = lazy_length.force(); | ||
REQUIRE(call_counter == 1); | ||
REQUIRE(result == 3); | ||
result = lazy_length.force(); | ||
REQUIRE(call_counter == 1); | ||
REQUIRE(result == 3); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not a public constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having a static function with a name is clearer in that case, when I read
lazyt::from_fun(f)
I know we use a functionf
to make alazyt
object, butlazyt(f)
is more obscure.