Think about the values you're reducing one by one:
>>> reduce(lambda x,y: isprime(x) and isprime(y), [2])
2
Only one element, so reduce
gives us the first.
>>> reduce(lambda x,y: isprime(x) and isprime(y), [2, 3])
True
Makes sense: both are prime.
>>> reduce(lambda x,y: isprime(x) and isprime(y), [2, 3, 5])
False
This one seems weird, but it's because you're actually calling
isprime(True) and isprime(5)
because True
is the last value, and True
has an int
value of 1:
>>> int(True)
1
reduce
!= all
.