Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 40375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:00:50+00:00 2026-05-10T15:00:50+00:00

Has anybody found a good solution for lazily-evaluated lists in Perl? I’ve tried a

  • 0

Has anybody found a good solution for lazily-evaluated lists in Perl? I’ve tried a number of ways to turn something like

for my $item ( map { ... } @list ) {  } 

into a lazy evaluation–by tie-ing @list, for example. I’m trying to avoid breaking down and writing a source filter to do it, because they mess with your ability to debug the code. Has anybody had any success. Or do you just have to break down and use a while loop?

Note: I guess that I should mention that I’m kind of hooked on sometimes long grep-map chains for functionally transforming lists. So it’s not so much the foreach loop or the while loop. It’s that map expressions tend to pack more functionality into the same vertical space.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:00:50+00:00Added an answer on May 10, 2026 at 3:00 pm

    As mentioned previously, for(each) is an eager loop, so it wants to evaluate the entire list before starting.

    For simplicity, I would recommend using an iterator object or closure rather than trying to have a lazily evaluated array. While you can use a tie to have a lazily evaluated infinite list, you can run into troubles if you ever ask (directly or indirectly, as in the foreach above) for the entire list (or even the size of the entire list).

    Without writing a full class or using any modules, you can make a simple iterator factory just by using closures:

    sub make_iterator {     my ($value, $max, $step) = @_;      return sub {         return if $value > $max;    # Return undef when we overflow max.          my $current = $value;         $value += $step;            # Increment value for next call.         return $current;            # Return current iterator value.     }; } 

    And then to use it:

    # All the even numbers between 0 -  100. my $evens = make_iterator(0, 100, 2);  while (defined( my $x = $evens->() ) ) {     print '$x\n'; } 

    There’s also the Tie::Array::Lazy module on the CPAN, which provides a much richer and fuller interface to lazy arrays. I’ve not used the module myself, so your mileage may vary.

    All the best,

    Paul

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

has anybody found a library that works well with large spreadsheets? I've tried apache's
Has anybody found a solution for this ? http://seanmonstar.com/post/709013028/ie-opacity-ignores-positioned-children I’ve been searching for hours
I am facing a similar problem to this question has anybody found a solution
I'm wondering if anybody's found a good solution to this: In our unit tests;
Has anybody found a fix for Visual Studio crashing with this error (eventlog)? .NET
Has anybody found any documentation on comments in jaxb.index files? I've searched the web,
Has anybody found examples of using FXG files in real world applications?
Has anybody found a way to make the GWT CellTable allow user resizing of
Has anybody found a workaround for Opera regarding Cross-Origin Resource Sharing? I want to
Does anybody know of a good common frameworks for Java that has common but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.