Skip to content

Commit

Permalink
Adding loop-return.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Jul 26, 2016
1 parent 89e0179 commit 48bcf97
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Example:

* v1.3 `loop-for-each-line` now works even if point moves
around. Inside `loop-for-each-line`, `it` is now set to the current
line.
line. Added `loop-return`.
* v1.2 Added `loop-for-each-line`. Also added edebug support, so you
can step through loops in loop.el.
* v1.1 Added `loop-continue`
Expand Down
7 changes: 6 additions & 1 deletion loop.el
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

;; Future ideas:

;; * loop-return
;; * Named loops so you can break/continue outer loops

(defmacro loop-while (condition &rest body)
Expand Down Expand Up @@ -112,5 +111,11 @@ If there are nested loops, breaks out of the innermost loop."
are nested loops, applies to the innermost loop."
(throw 'loop-continue nil))


(defun loop-return (value)
"Terminate evaluation of a `loop-while', `loop-do-while', or `loop-for-each' block.
The return value from the loop is VALUE."
(throw 'loop-break value))

(provide 'loop)
;;; loop.el ends here
9 changes: 9 additions & 0 deletions test/loop-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@
(setq sum (+ sum x)))
(should (equal sum 13))))

(ert-deftest loop-test-for-each-return ()
"`loop-return' should let us return values from a loop."
(should
(equal
3
(loop-for-each x (list 1 2 3 4)
(when (equal x 3)
(loop-return x))))))

(ert-deftest loop-test-for-each-line ()
(with-temp-buffer
(insert "foo\nbar\nbaz")
Expand Down

0 comments on commit 48bcf97

Please sign in to comment.