Arc Forumnew | comments | leaders | submitlogin
[patch] Break out of loops early
1 point by sjs 5891 days ago | 4 comments
Everyone wants the freedom to break out of a loop now and then. Arc's point macro makes this trivial to add, we just need to move said macro from the bottom to the top of arc.arc and then actually use it.

I just committed this to the git repo. Here's a patch if you're not using git: http://samhuri.net/arc-break.patch



2 points by almkglor 5890 days ago | link

Hmm. Personally, if I wanted to do this, I'd do the point macro myself manually, i.e.

  (point return
    (while (something)
        (aif (something-else) (return it))))
The problem, you see, is breaking out of nested loops:

  (point return
     (while (something)
        (if (some-condition)
            (while (something-in-condition)
               (aif (some-other-condition) (return it))))))
Personally, while I do find this quite a good idea, I think it happens rarely enough that the 'point trick should be recommended in a tutorial, but not build-in a (point break ...) in each loop. Just my gut feel though.

-----

1 point by sjs 5890 days ago | link

I was thinking the same thing this morning actually. Probably not something common enough to warrant wrapping every loop. I like the breakable idea of yours.

-----

2 points by almkglor 5889 days ago | link

Could be implemented. I'm in the office right now so I can't use arc here (mandatory windows pc, not to mention a shared internet terminal); it would be possible to just add it into arc.arc, although with a seriously good docstring.

  (mac breakable (expr)
    " Creates a control structure which can be exited by
      calling `break' with a value to be returned.  To
      use, add `breakable:' before the control structure,
      e.g. (breakable:while t (aif (something) (break it)))
      See also [[catch]] [[point]] [[compose]] [[while]] "
      `(point break ,expr))

-----

2 points by almkglor 5890 days ago | link

Hmm. How about an alternative suggestion?

Instead we define this:

  (mac breakable (exp)
      `(point break ,exp))
And we make loops and other control structures that support break with this:

  (breakable:while (condition)
        (aif (something) (break it)))

-----