List::...::Cycle(3pm)
π NAME π
List::Rotation::Cycle β cycle through a list of values via a singleton object implemented as closure. π β¨
πΈ SYNOPSIS πΈ
use List::Rotation::Cycle;
my @array = qw( A B C );
my $first_cycle = List::Rotation::Cycle->new(@array);
my $second_cycle = List::Rotation::Cycle->new(@array);
print $first_cycle->next; ## prints A
print $second_cycle->next; ## prints B
print $first_cycle->next; ## prints C
print $second_cycle->next; ## prints A, looping back to beginning
okay bestie look at that last bit closely because it's giving β¨plot twistβ¨ β those two "different" objects are literally the same object and that's the whole point π«Ά
π¦ DESCRIPTION π¦
So List::Rotation::Cycle is your girl for looping through a list of values π Once you hit the end of the list, you just... go back to the beginning. Round and round, no cap. π
And here's the iconic part: List::Rotation::Cycle is implemented as a Singleton Pattern. You always just get 1 (the very same) Cycle object even if you use the new method several times. Yes, several times. Doesn't matter. Same object. Same one. π
How does she pull that off? By using Memoize on the new method, bestie. It returns the same object for every use of new that comes with the same List of parameters. π So same params in β same exact object out. Every single time. Main character energy fr fr π
ngl this is a big deal: if you're out here expecting two separate, independent cycles from two separate new calls, the universe said no. π
π OBJECT METHODS π
newβ Create a Cycle object for the list of values in the list. β¨ (and remember, the memoization vibe means repeated calls with the same list hand you back the identical object π«Ά)nextβ Return the next element. This method is implemented as a closure. π So it's holding onto its own little state bubble and just yeets the next value at you every time you call it. Neat.
π AUTHOR π
Imre Saling, "<pelagicatcpandotorg>" π
β COPYRIGHT and LICENSE β
Copyright 2000-2004, Imre Saling, All rights reserved. πΈ
This software is available under the same terms as perl. πͺ
okay that's the whole page, bestie, you ate it π This lovely little doc came to you from perl v5.36.0, dated 2022-10-15 π¦ go forth and cycle responsibly β¨π
π¦ where this page comes from
- ubuntu noble: liblist-rotation-cycle-perl 1.009-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: liblist-rotation-cycle-perl 1.009-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: liblist-rotation-cycle-perl 1.009-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms