Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Unconditional loops are unconditionally awesome (brson.github.io)
10 points by akbarnama on Jan 18, 2021 | hide | past | favorite | 11 comments


If you are using C and want the same form of "satisfaction" then just do #define loop while(1)


#define ever ;;

for(ever){}


Couldn't you do this in any language? Either write a do while or write a infinite loop with true as condition and then write breaks in the loop itself?

That's what I mostly do in java, write a while(true) and then either come back to change the condition or write break in the loop.


One thing I don't know how to do in Python is using while loops to collect results of some other loop that runs at the same time.

For example, within the same script, I would have a simulate_magic_numbers function with a loop that produces a sequence of magic numbers continuously for a few hours and returns them once done. Below, I would like to have a while loop that grabs the magic numbers every 10 minutes and plots them.

I know how to do it with two separate scripts and saving intermediate numbers to a file, but it would be great to be able to do it within a single Jupiter notebook. However, there doesn't seem to be a straight-forward way of doing it.


Seems like you just want a global variable that is shared between two cells?

    current_items = None

    def generate_numbers():
        global current_items
        items = []
        current_items = items
        while True:
            items.append(my_item)
        return items

    def view_current_plot():
        plot(current_items)
Note that you probably want to run `generate_numbers()` in a different thread or a different async coroutine so the Jupyter kernel would not block for the duration of it.


You could use a co-routine to generate the magic numbers. Something like this:

  def gen_magic_nums():
     for i in range(100):
        yield i**2

  for num in gen_magic_nums():
     add_to_plot(num)


The problem is we don't always write perfect code, so if you forget to return or break on any given branch (I.e. in some else statement) you could get stuck in the loop forever. Remember Apple's famous goto fail; SSL bug? Sometimes it's not so easy to eyeball the code and follow the execution flow.

Specifying the condition for the loop up front makes me feel more confident about the loop safely concluding once the work is done.


It has its uses but it's pretty low hanging fruit. I also think having the termination conditions in the loop header is much more readable most of the time


I have written very little rust code, but loop is probably the thing I like the least. It requires mutation to be useful in most cases, even for things where you could avoid it. I would much rather have something like scheme's named let (or a decorator to enforce LLVMs "sibling recursion").


While JS doesn’t have native support I do this with while (true) and a break clause. It’s very useful at times.


Drop the condition and now you have unconditional loop: for(;;){}




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: