compose
Jp Calderone
exarkun at intarweb.us
Mon May 5 03:15:04 EDT 2003
On Mon, May 05, 2003 at 04:56:32AM +0000, xam wrote:
> i've been truing to get this 'simple' code to work,
> def compose(*funcs):
> ... if len(funcs)>1:
> ... return lambda x:funcs[0](compose(funcs[1:])(x))
> ... else: return lambda x:funcs[0](x)
> >>> compose(add_5, mul_3, sub_2)(2)
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> File "<interactive input>", line 3, in <lambda>
> File "<interactive input>", line 4, in <lambda>
> TypeError: 'tuple' object is not callable
> does anyone want to give it a shot?
>
compose(add_5, mul_3, sub_2) ->
lambda x: add_5(compose((mul_3, sub_2))(x) ->
lambda x: (mul_3, sub_2)(x)
You're missing the extra, magic *:
return lambda x: funcs[0](compose(*funcs[1:])(x))
Jp
--
#!/bin/bash
( LIST=(~/.sigs/*.sig)
cat ${LIST[$(($RANDOM % ${#LIST[*]}))]}
echo -- $'\n' `uptime | sed -e 's/.*m//'` ) > ~/.signature
--
up 3 days, 4:38, 7 users, load average: 0.14, 0.10, 0.09
More information about the Python-list
mailing list