I'm trying to construct a function in Python that accepts a list of numbers and returns a list of functions dependent on the input values:
filters = lambda ys: [lambda x: x % y == 0 for y in ys]
If I call the above filters
function with a list of values, it should return a list of lambda functions which have been constructed from the input list. For example, calling filters([3,5])
should return
[lambda x: x % 3 == 0,
lambda x: x % 5 == 0]
Defining another function, applyToNum
(similar to map
) as
applyToNum = lambda x, fns: [fn(x) for fn in fns]
I now encounter the following behaviour:
applyToNum(15, filters([3,4,5]))
# returns [True, True, True] rather than [True, False, True]
The interesting thing is that if I construct the filters
function list manually, it works as expected:
filters2 = [
lambda x: x % 3 == 0,
lambda x: x % 4 == 0,
lambda x: x % 5 == 0]
applyToNum(15, filters2)
# returns [True, False, True]
Could someone explain why the above behaviour is seen?