conditionals in lambdas?
Ben Wolfson
wolfson at midway.uchicago.edu
Thu Nov 9 14:27:50 EST 2000
In article <psgl0t4bvnm5o76phoaqqldrrs44uoafmh at 4ax.com>,
Steve Horne <sh at ttsoftware.co.uk> wrote:
>I agree with most of what you say, and I confess I wasn't thinking of
>it. Also, I have no idea why I suddenly thought list subscripts
>started at 1.
>
>However, did you realise how easy a lazy version is to create...
>
>Using tuples (which do seem clearer than my list version, and probably
>better in other ways to)...
>
> (no_value,yes_value) [condition]
>
>This can be adapted as follows...
>
> ((lambda : no_value, lambda : yes_value) [condition]) ()
>
>That is, the indexing operation selects which lambda to evaluate.
>
>Of course the lambdas may need some default parameters, and could
>easily get a bit awkward syntactically. But it should work.
>
>So with the case using division...
>
> ((lambda : 0, lambda j=j : 1/j) [j != 0]) ()
Or:
>>> from lambdabuse import If
>>> (lambda: If(0, lambda: 1/0, If(1, 1, lambda: 1/0)))()
1
--
BTR | It is a symptome of Melancholy to be afraid of death, and yet
sometimes to desire it; this latter I have often discovered in my selfe, and
thinke noe man ever desired life as I have sometimes Death.
-- Thomas Browne, _Religio Medici_
More information about the Python-list
mailing list