conditionals in lambdas?
Steve Horne
sh at ttsoftware.co.uk
Thu Nov 9 10:45:09 EST 2000
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]) ()
--
Steve Horne
sh at ttsoftware.co.uk
More information about the Python-list
mailing list