Skip to content

Instantly share code, notes, and snippets.

@copperwall
Created May 17, 2017 16:55
Show Gist options
  • Save copperwall/5a6b1c27d4095724e23a44663bc3f1be to your computer and use it in GitHub Desktop.
Save copperwall/5a6b1c27d4095724e23a44663bc3f1be to your computer and use it in GitHub Desktop.
Lazy Evaluation
<?php
// User Generator
function getGuideAuthors(int $guideid): Iterator {
// Guide::getUserids could be a generator too!
foreach (Guide::getUserids($guideid) as $userid) {
// Yield full ORM object.
yield User::get($userid);
}
}
function notifyUser(int $guideid, Iterator $users): void {
$guide = Guide::get($guideid);
if (!$guide->public) {
// If the guide isn't public, no Users are generated.
return;
}
foreach ($users as $user) {
sendNotification('stuff', $user);
}
}
$authorGen = getGuideAuthors(13470);
notifyUsers(13470, $authorGen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment