10 Jun
2018
10 Jun
'18
3:06 a.m.
Hi, I have found bug in functools.reduce (Python V 3.6.5). The function ignores the value of initializer, if initializer is zero. Below is the example code import functools as f ar = [2, 5, 4, 1, 3] out = f.reduce(lambda prev, curr: prev < curr and prev or curr, ar, 0) print(out)
Expected: 0 Actual: 1
out2 = f.reduce(lambda prev, curr: prev < curr and prev or curr, ar, -1) print(out2)
Expected: -1 Actual: -1
Best Regards, Witthawat P.
11 Jun
11 Jun
10:18 p.m.
It's not a bug. Since 0 is falsy value, `0 or 2` is 2, not 0. You need to use `prev if prev < curr else curr` instead of `cond and A or B` hack.
2399
Age (days ago)
2400
Last active (days ago)
1 comments
2 participants
participants (2)
-
INADA Naoki
-
Witthawat P.