List::...::Cycle(3pm)

πŸ“¦
version 1.009 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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 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