473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

define loop statement?

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
Feb 16 '06 #1
17 3063
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)

Feb 16 '06 #2
David Isaac wrote:
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
Feb 16 '06 #3
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 :-)

--
René Pijlman
Feb 16 '06 #4
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.o rg) 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

Feb 17 '06 #5
Rene Pijlman wrote:
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 :-)

Or by hacking through the Python source and creating his own "somehow
pythonish but absolutely not python" language
Feb 17 '06 #6
Rene Pijlman wrote:
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 :-)

Or by hacking through the Python source and creating his own "somehow
pythonish but absolutely not python" language
Feb 17 '06 #7
David Isaac wrote:
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"
Feb 18 '06 #8
Jeffrey Schwab wrote:
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
Feb 18 '06 #9
Jeffrey Schwab wrote:
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()".
print "OK"

Feb 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
12067
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) { int result = 0; for(int i = 1;i <=iterations;i++) { result += i;
13
7652
by: Vinu | last post by:
The following code doesn't prints anything why it is? The code is correct. plz explain the logic #include <stdio.h> int arr = {10,20,30,40,50}; #define ARR_SIZE sizeof(arr)/sizeof(arr) void main()
3
3511
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once, while the C# for loop ending condition is evaluated on every iteration." Is this accurate? I don't understand how you could get away without...
7
2905
by: gmou | last post by:
Dear group, I am building a translator from C++ into VB (and into C#). At the moment, I have a hard time figuring out the equivalent of a 'for' loop in VB. Given C++ code: for( int i=0; test_fct(i); increment_fct(i) ) { ... // body of the loop }
34
2663
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include <limits.h> #include <stdlib.h> int main(void) {
14
5885
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
17
2762
by: niraj.tiwari | last post by:
What is meaning of the following define:- #define x(argl...) x1(##argl)
6
1868
by: raghu | last post by:
#define GOOGLE int main(void) { printf("%d",GOOGLE); return 0; } In the above program ,by default GOOGLE should be assigned to zero..right? But when I try to print it it gives an error at printf
16
1751
by: Andy B | last post by:
I have the following code inside of a WebBrowser.DocumentCompleted event: For index As Integer = 0 To Me.Browser.Document.GetElementsByTagName("ul").Item(0).GetElementsByTagName("li").Count NotesList.Items.Add(Me.Browser.Document.GetElementsByTagName("ul").Item(0).GetElementsByTagName("li").Item(index).InnerText) Next index
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.