[TUHS] 80 columns ...

Random832 random832 at fastmail.com
Sun Nov 12 03:30:20 AEST 2017


On Sat, Nov 11, 2017, at 12:04, Ian Zimmerman wrote:
> On 2017-11-10 13:21, Norman Wilson wrote:
> > -- I miss one particular case of assigment having a value:
> > that of
> > 	while ((val = function()) != STOP)
> > 		do something with val
> 
> I was once in a remote job interview with a Ruby shop.  I don't know
> Ruby, but they said I could use Python.  Of course this situation came
> up (it's pretty common when you think about it) and on this occasion a
> whim made me write it thus:
> 
> while True:
>     val = function()
>     if val == STOP:
>         break
>     do_something()
> 
> Their reply was overflowing with shock and horror that I would use
> "while True", and that was the end of that opportunity for me.
> Apparently Ruby has a construct to handle this cleanly, without having
> to call function() from two sites.

Python has one as well, though it's a bit obscure - ISTR around half of
the people who commented on a thread on the python mailing lists where
this came up weren't familiar with it.

for val in iter(function, STOP):
    do_something()

If the function needs arguments, you have to use a lambda. If the
condition is something other than equality to some value... well, you
can hack something together, but it's debatably cleaner than the while
True loop.

(If the function is readline, though, you can just iterate over the file
object)


More information about the TUHS mailing list