I've recently made the following example for Pythons for ... else:
def isPrime(element):
""" just a helper function! don't get religious about it! """
if element == 2:
return True
elif element <= 1 or element % 2 == 0:
return False
else:
for i in xrange(3, element, 2):
print i
if element % i == 0:
return False
return True
myList = [4, 4, 9, 12]
for element in myList:
if isPrime(element):
break
else:
print("The list did not contain a prime.")
A fellow student told me, that this task can be done with Scala like this:
List(4, 4, 9, 12) exists isPrime
Which gets lazy evaluated.
Does something similar like the exists-keyword exist in Python? Or is there a PEP for that?