define loop statement?

D

David Isaac

I would like to be able to define a loop statement
(nevermind why) so that I can write something like

loop 10:
do_something

instead of

for i in range(10):
do_something

Possible? If so, how?

Thanks,
Alan Isaac
 
J

Jonathan Gardner

No, not in the way you think it is. What you can do instead is
something like this:

def do_something(i):
... do_something ...

def loop(n, func):
for i in range(n): func(i)

loop(10, do_something)
 
R

Rene Pijlman

David Isaac:
I would like to be able to define a loop statement
(nevermind why) so that I can write something like

loop 10:
do_something

instead of

for i in range(10):
do_something

Possible? If so, how?

Yes. By implementing a compiler or an interpreter for your programming
language. Or a preprocessor that converts your language to Python, or some
other suitable intermediate language. Or a programmer, that converts your
pseudocode and some coffee to the desired algorithm :)
 
G

Georg Brandl

David said:
I would like to be able to define a loop statement
(nevermind why) so that I can write something like

loop 10:
do_something

instead of

for i in range(10):
do_something

Possible? If so, how?

It's not possible to create a new statement, with suite
and indentation rules without hacking the interpreter or
resorting to alternative bytecode compilers such as "pyc".

Creating a _function_ named "loop" is easy as Jonathan's
answer shows.

Georg
 
B

bearophileHUGS

David Isaac:
I would like to be able to define a loop statement
(nevermind why) so that I can write something like

loop 10:
do_something

instead of

for i in range(10):
do_something

Possible? If so, how?

It seems that you are looking for macros; maybe Logix "language"
(www.livelogix.net/logix) or Boo (boo.codehaus.org) can solve your
problem.
Otherwise you can do it with very different languages like Lisp or
Scheme (with a different syntax) and maybe Dylan too.

If you tell us your purpose, maybe we can suggest you a
better/different solution.

Bye,
bearophile
 
X

Xavier Morel

Rene said:
David Isaac:

Yes. By implementing a compiler or an interpreter for your programming
language. Or a preprocessor that converts your language to Python, or some
other suitable intermediate language. Or a programmer, that converts your
pseudocode and some coffee to the desired algorithm :)
Or by hacking through the Python source and creating his own "somehow
pythonish but absolutely not python" language
 
X

Xavier Morel

Rene said:
David Isaac:

Yes. By implementing a compiler or an interpreter for your programming
language. Or a preprocessor that converts your language to Python, or some
other suitable intermediate language. Or a programmer, that converts your
pseudocode and some coffee to the desired algorithm :)
Or by hacking through the Python source and creating his own "somehow
pythonish but absolutely not python" language
 
J

Jeffrey Schwab

David said:
I would like to be able to define a loop statement
(nevermind why) so that I can write something like

loop 10:
do_something

instead of

for i in range(10):
do_something

Possible? If so, how?

Ruby and Smalltalk are both good at this kind of thing, since they have
syntactic support for associating a block with each method call. In
Python, I think you just have to do a little more setup. How about
something like this?

class Loop:
def __init__(self, n):
self.n = n
def __call__(self):
self.n = self.n - 1
return self.n != 0


if __name__ == '__main__':
loop = Loop(10)
while loop:
print "OK"
 
G

Georg Brandl

Jeffrey said:
class Loop:
def __init__(self, n):
self.n = n
def __call__(self):
self.n = self.n - 1
return self.n != 0


if __name__ == '__main__':
loop = Loop(10)
while loop:
print "OK"

Seems you forgot "()" after "while loop" above.

Georg
 
J

Jeffrey Schwab

Jeffrey said:
class Loop:
def __init__(self, n):
self.n = n
def __call__(self):
self.n = self.n - 1
return self.n != 0


if __name__ == '__main__':
loop = Loop(10)
while loop:

Whoops. Should be "while loop()".
 
D

David Isaac

Jeffrey Schwab said:
class Loop:
def __init__(self, n):
self.n = n
def __call__(self):
self.n = self.n - 1
return self.n != 0


if __name__ == '__main__':
loop = Loop(10)
while loop():
print "OK"

OK, that's pretty good.
Thanks!
Alan Isaac
 
F

Felipe Almeida Lessa

Em Sáb, 2006-02-18 às 20:04 +0000, Jeffrey Schwab escreveu:
if __name__ == '__main__':
loop = Loop(10)
while loop:
print "OK"

Maybe:

while Loop(10)():
print "OK"

Looks rather ugly but requires one less line ;-).

--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"
 
N

Nigel Rowe

Felipe said:
Em Sáb, 2006-02-18 às 20:04 +0000, Jeffrey Schwab escreveu:

Maybe:

while Loop(10)():
print "OK"

Looks rather ugly but requires one less line ;-).
Doesn't work. You get a NEW Loop(10) instance on each pass through the
'while'. This is just an expensive way to make an endless loop.
 
F

Felipe Almeida Lessa

Em Dom, 2006-02-19 às 11:08 +1100, Nigel Rowe escreveu:
Doesn't work. You get a NEW Loop(10) instance on each pass through the
'while'. This is just an expensive way to make an endless loop.

Oh, sorry, ignore me on that one. Now I think I should sleep =S...

--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"
 
B

Benji York

David said:
I would like to be able to define a loop statement
(nevermind why) so that I can write something like

loop 10:
do_something

Here's a flagrant hack:

import sys

VAR_NAME = '__repeat_counter'

def set_repeat_counter(value):
frame = sys._getframe(2)
frame.f_locals[VAR_NAME] = value

def get_repeat_counter(value):
frame = sys._getframe(2)
if VAR_NAME not in frame.f_locals:
frame.f_locals[VAR_NAME] = value

return frame.f_locals[VAR_NAME]

def repeat(limit):
set_repeat_counter(get_repeat_counter(limit)-1)
return get_repeat_counter(limit)

while repeat(10):
print 'OK'

Without more work it doesn't allow nested loops though.

And for the record, if you're worrying about Python's counted loop
construct you need better things to worry about.
<insert-smilies-as-appropriate>
 
D

David Isaac

Benji York said:
Here's a flagrant hack:

Admiration wins out over revulsion. ;-)
Thanks,
Alan Isaac

PS Here's the motivation. Python closely resembles pseudocode. With
a very little LaTeX hacking, it is often possible to write algorithms is
Python that typeset as reasonable pseudocode. A simple repetitive
loop is a bit of a sticking point.
 
C

Cameron Laird

.
.
.
Admiration wins out over revulsion. ;-)
Thanks,
Alan Isaac

PS Here's the motivation. Python closely resembles pseudocode. With
a very little LaTeX hacking, it is often possible to write algorithms is
Python that typeset as reasonable pseudocode. A simple repetitive
loop is a bit of a sticking point.

Wow. Innovative.
 
M

Magnus Lycka

David said:
PS Here's the motivation. Python closely resembles pseudocode. With
a very little LaTeX hacking, it is often possible to write algorithms is
Python that typeset as reasonable pseudocode. A simple repetitive
loop is a bit of a sticking point.

With slightly more LaTeX hacking or possibly Python hacking on your
script before passing it to LaTeX, you might just be able to change
occurences of

for x in range(a, b):

to

for x in [a, b):

or whatever, and get away with plain vanilla Python
in your code...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top