I'm messing with python, and I'm looking for a way to replicate a for loop in a lambda.
Basically, I'd like to convert the function below to a lambda doing the same thing :
def basicForLoop(x):
for i in range(x):
print(i)
basicForLoop(100)
For now, I've managed to do it by using recursion and increasing the value to each new recursion :
(lambda f: lambda x: f(f,0, x))(lambda f,current,max: print(current) or f(f, current+1, max) if current <= max else None)(100)
This work rather well, but it hit the max recursion depth as soon as the number start to be too big, so I'm looking for a way to rearrange this lambda so that it can be used without worrying about the recursion depth to make it truly equivalent to the original function.
EDIT : I'm looking for a way to do this while keeping the loop logic directly inside the lambda, delegating the loop to another function like map, join, ... isn't what I'm looking for.
PS. I know very well that this is an abomination that should never be used in reality but I'm just curious about it.
(lambda x: any(map(print, range(x))))(10)?(lambda x: any(print(i) for i in range(x)))(10). I'm usingany()as a memory-efficient way to consume generator. You can simply unpack it or turn into a list comprehension, but it will return list of NNoneelements.oras a way to just sequence multiple statements within the body of a lambda?