-
-
Notifications
You must be signed in to change notification settings - Fork 149
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
Added Nette\Utils\Future for lazy value evaluation #272
Open
milo
wants to merge
25
commits into
nette:master
Choose a base branch
from
milo:pull-future
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…turn objects Type (BC break)
I like it. But it really should have better name. It basically just maps/binds values from data source into. Maybe something like LazyMapper? LazyBinder? |
Yep, the hardest thing :) |
Deferred? Delayed? |
I think there's a missing $futurePersons = new Future(function (array $ids) use ($db) {
return $db->query('SELECT id, first_name, last_name FROM person')->fetchAssoc('id');
}); |
dg
force-pushed
the
master
branch
12 times, most recently
from
June 18, 2024 21:22
6733224
to
5de10a1
Compare
dg
force-pushed
the
master
branch
16 times, most recently
from
August 7, 2024 16:18
a846fab
to
736c567
Compare
dg
force-pushed
the
master
branch
2 times, most recently
from
December 12, 2024 10:02
cd9170e
to
2b48b24
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
Future
class is a helper for "building data structures" which I use about 4 years. It is handy for small tasks where ORM is too huge, or where two independent data sources are mixing up (database and database, HTTP API and database, local CSV and HTTP API...).I used to call it "promise" but it is not a promise pattern. It is closer to "future evaluation strategy" but it is not the same. Maybe there exists the corrent name for this evaluation method but I didn't find it.
An example of usage:
On
resolve()
call, the resolver (callback from constructor) is called with all required authors and editors ID. So there is no need to iterate over books, collects every ID, fetch them and iterate again to build the desired data structure.The example with database is a little bit funny (even I'm using it in this way). Some ORM does this task probably better. But this Future class is pretty low level helper. If you compose data structures from heterogeneous sources, like files in filesystem, HTTP API or some other JSON sources, the resulting code is nice, short and clean.
Another use case is scalar to value object translation. For example, translate every e-mail string, to Email object:
Or scalar to scalar translation (language translator).
Anyway, the workflow is following:
and API looks like:
These are common use cases I hit and examples can be found in attached test.
In general, there is a space for
->bindAssoc('a[]->foo->bar', $structure)
but I rarely use it. It could be nice but if required, manual iteration through the$structure
and->bind...()
calls work too.One dark side - it is quite hard to goole
future
term so maybe different name should be used.