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 6960919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:27:48+00:00 2026-05-27T15:27:48+00:00

Suppose I have a function which consults some external stateful service and returns a

  • 0

Suppose I have a function which consults some external stateful service and returns a value from it, for simplicity let’s assume the value is an integer:

i = 10
def foo():
    global i
    i -= 1
    return i

It’s clear that I can call this function 9 times before it returns a falsy value (the 10th call will return 0 which will evaluate to false in boolean contexts). With some function that works this way, I can now “iterate” it by wrapping it in a generator:

def take_while_truthy(func):
    while True:
        nextval = func()
        if not nextval: 
            break
        yield nextval

And then:

for x in take_while_truthy(foo):
    print x

gives me:

9
8
[...]
2
1

My question is: is there a higher-order function somewhere in the standard library which does this or something like it? I skimmed itertools and functools but didn’t find anything quite like what I want to do. Have I missed something?

  • 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. Editorial Team
    Editorial Team
    2026-05-27T15:27:49+00:00Added an answer on May 27, 2026 at 3:27 pm

    This can actually be done using the built-in iter(), if you provide two arguments instead of one then the first is expected to be a function that will be called repeatedly until the return value of the function reaches the sentinel value:

    for x in iter(foo, 0):
        print x
    

    Here is what the itertools solution might look like, iter() is definitely cleaner but this allows you to test for any falsy return:

    from itertools import takewhile, imap, repeat
    for x in takewhile(bool, imap(lambda f: f(), repeat(foo))):
        print x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an array of values [a,b,c,d,...] and a function f(x,...) which returns
Suppose I have a function like this: def getNeighbors(vertex) which returns a list of
Suppose I have some kind of factory function which creates objects that are largely
Let's suppose I want to create a javascript class/object/function which have a method that
Suppose I have a function which takes some form of predicate: void Foo( boost::function<bool(int,int,int)>
Suppose I have a function which looks like this: template <class In, class In2>
Suppose I have a function which can either take an iterable/iterator or a non-iterable
Suppose I have a function which takes variadic arguments ( ... ) or a
Suppose you have a simple function, which can get quite expensive for large values:
Suppose you have a function 'normalize' which takes a list of numbers (representing a

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.