4 ms·
from functools import reduce from operators import mul def times(data): return reduce(mul, data)
by TheLoneWolfling 11y ago
from functools import reduce
from operators import mul
def times(data):
return reduce(mul, data)
- agumonkey 11y agoI'm not saying it's not possible, just that they stopped too early. >>> times([]) TypeError: reduce() of empty sequence with no initial value from functools import reduce from operators import mul def times(data): return reduce(mul, data) Proper neutral element: >>> times = lambda l: reduce(mul, l,1) >>> times([]) 1
- TheLoneWolfling 11y agoWhoops, but again as you say that is trivial to fix.
- agumonkey 11y agoIt's more about the philosophy, some languages recognize this way of thinking, having tiny bits of ~monoid-ic logic that works for N. Lists have neutral element (nil) and concatenative operation (cons). Arithmetic operations follow that. IMO they don't approach systems the same way.