I have the certain limitation in my project that I cannot use statements/keywords like while
, so I'm wondering that whether there exists a function that works similar to the below code example in the standard library i.e. functools
, itertools
etc.?
>>> # signature: loop_while(pred, g, f, *args, **kwargs)
>>> loop_while(lambda r: r != 0, # predicate
... lambda v: v - 1, # modifier function
... lambda: 5 # initial function
... )
Where loop_while
could be defined as something roughly similar to
def loop_while(pred, g, f, *args, **kwargs):
res = f(*args, **kwargs)
while pred(res):
res = g(res)
Which is functionally equivalent to:
n = 5
while n != 0:
n -= 1
Or another solution would be one that allows you to do a predicated-loop in one line e.g. [<expr> while <pred>]
but using any tricks that are possible.