map/filter/reduce/lambda opinions and background unscientific mini-survey
Ron Adam
rrr at ronadam.com
Wed Jul 6 12:30:49 EDT 2005
Devan L wrote:
>># from a custom numeric class
>># converts a tuple of digits into a number
>>mantissa = sign * reduce(lambda a, b: 10 * a + b, mantissa)
>
>
> I'll admit I can't figure out a way to replace reduce without writing
> some ugly code here, but I doubt these sorts of things appear often.
It's not ugly or difficult to define a named function.
def digits_to_value(seq):
v = 0
for d in seq:
v = v*10+d
return v
Then where you need it.
mantissa = sign * digits_to_value(mantissa)
One of the motivations is the reduce-lambda expressions are a lot harder
to read than a properly named function.
And a function will often work faster than the reduce-lambda version as
well.
Cheers,
Ron
More information about the Python-list
mailing list