Iterator(3)

πŸ“¦
version 0.3 Β· 4 versions
, in epel 9

πŸ¦‹ Iterator(3) β€” like, the manual page, but make it make sense πŸ’…

okay so this is a page from the User Contributed Perl Documentation (yes that's the manual name, it's giving ✨community vibes✨, no cap) and it's all about Class::Iterator, which is literally the moment closures decided to put on a little object-shaped outfit. periodt.


✨ NAME ✨

Class::Iterator β€” it's an Iterator class, bestie. that's the whole name. iconic of it honestly πŸŽ€


🌸 SYNOPSIS 🌸

here's the vibe, copy-paste ready, character for character because I would never do you like that:

  use Class::Iterator;
  my $it = Class::Iterator->new(\&closure_generator);

  while (my $v = $it->next) { print "value : $v\n" }

  # use map like
  my $it2 = imap { ...some code with $_...} $it
  while (my $v = $it->next) { print "value : $v\n" }

  # use grep like
  my $it3 = igrep { ...some code with $_...} $it
  while (my $v = $it->next) { print "value : $v\n" }

like… imap and igrep?? the way that's just map and grep but for iterators 😭 obsessed.


πŸ’– DESCRIPTION πŸ’–

so Class::Iterator is a generic iterator object class. that's the official description, I'm not making it up 🫢. what it does is take a closure and wrap it up into an object interface β€” it's literally giving "closure but make it OO". main character energy fr fr.

  • new(\&closure_generator) β€” this is the constructor πŸŽ€. the argument is a sub that looks like this:
  sub closure_generator {
    my $private_data;
    return sub {
      # do something with $private_data
      # and return it
      return $private_data
    } }

like, you hand it a generator sub, and it hands you back an object. slay.

  • next β€” calling this method makes one iteration. one step of the loop, one little next moment, and you're moving on πŸ’«
  • $o = imap { ... } $it β€” this is a creator ✨. it creates a new iterator from an existing iterator, in the manner of map. same energy as map, just iterator-flavored.
  • $o = igrep { ... } $it β€” also a creator 🌈. it creates a new iterator from an existing iterator, in the manner of grep. filtering but make it lazy, iykyk.

πŸ‘‘ CREDITS πŸ‘‘

shoutout time, we love to see it πŸ’œ:

  • Marc Jason Dominius's YAPC::EU 2003 classes.
  • Ken Olstad

(no, I didn't fix the spelling of anything, I keep the facts exactly as they came, that's the rule bestie 🫢)


🧚 AUTHOR 🧚

Robert Silve <robert@silve.net> β€” thank you for your service, king πŸ‘‘


reminder: every option, name, path, and line above is verbatim from the original page. the vibes are new, the facts are not. πŸ’…

🌸 and that's the whole page!! footer says it's from perl v5.32.1, dated 2022-01-10 β€” so go forth, iterate responsibly, and remember: your closure deserves an object interface too πŸ¦‹πŸ’–βœ¨

πŸ“¦ where this page comes from