Below I have a rough implementation of testing a list lambda functions on an integer in Python. I know Java currently doesn’t support closures or lambdas (yet), but I’m curious, is this remotely possible in Java?
pos = [lambda x : x > 0, "pos"]
even = [lambda x : x % 2 == 0, "even"]
small = [lambda x : x < 100, "small"]
def pass_test(x, tests):
if (len(tests) == 0): return []
else:
if (tests[0][0](x)): return [tests[0][1]] + pass_test(x, tests[1:])
else: return pass_test(x, tests[1:])
Yes, it is possible, along the lines of:
You see, this is a bit verbose, but it does the job.