The Tale of Anemon
The little anemon who likes to eat.
I am currently experimenting with the IteratorAggregate
interface to allow Anemon
instances to be used as ordinary arrays. This is interesting because we can shorten the API by implementing IteratorAggregate
to the Anemon
class. As in version 2.0.0
, we need to do this extra vomit
thing to randomize an array before iterating it afterwards:
$random = Anemon::from([1, 2, 3, 4, 5, 6])->shake()->vomit();
foreach ($random as $v) {
$color = '#' . substr(dechex(crc32($v)), 0, 6);
echo '<span style="background:' . $color . '">' . $color . '</span>';
}
But now, we can simply do this:
$random = Anemon::from([1, 2, 3, 4, 5, 6])->shake();
foreach ($random as $v) {
$color = '#' . substr(dechex(crc32($v)), 0, 6);
echo '<span style="background:' . $color . '">' . $color . '</span>';
}
This also applies to other methods such as sort
, is
, not
, etc:
foreach (Anemon::from([1, 2, 3])->sort(-1) as $v) { … }
My next plan is to convert the global $pages
variable into an Anemon
instance, so that next time we can do more on this variable as follows:
foreach ($pages as $page) { … }
foreach ($pages->shake() as $page) { … }
foreach ($pages->sort([1, 'title']) as $page) { … }
// Add sticky post
if ($site->is('home') && $pages = Lot::get('pages')) {
$pages->prepend(new Page(LOT . DS . 'page' . DS . 'sticky.page'));
Lot::set('pages', $pages->chunk($site->page('chunk'), $url->i ?: 1));
}
// Conditionally load the asset
if ($pages = Lot::get('pages')) {
if ($pages->any(function ($page) {
return $page->type === 'Gallery';
})) {
Asset::set('.\lot\asset\css\type.gallery.css');
}
} else if ($page = Lot::get('page')) {
if ($page->type === 'Gallery') {
Asset::set('.\lot\asset\css\type.gallery.css');
}
}
We can even apply it to page properties:
echo $page->tags->map(function ($tag) {
return HTML::a($tag->title, $tag->url, false, ['rel' => 'tag']);
});
0 Comments
No comments yet.