branch: externals/queue
commit 1c230d7a92d8972957fe73e77c9b05beaae113cf
Author: Toby S. Cubitt <[email protected]>
Commit: Toby S. Cubitt <[email protected]>
Implement iterator generators on collection data structures.
---
queue.el | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/queue.el b/queue.el
index 4c9c954..ca9f5ab 100644
--- a/queue.el
+++ b/queue.el
@@ -1,6 +1,6 @@
;;; queue.el --- Queue data structure -*- lexical-binding: t; -*-
-;; Copyright (C) 1991-1995, 2008-2009, 2012 Free Software Foundation, Inc
+;; Copyright (C) 1991-1995, 2008-2009, 2012, 2017 Free Software Foundation,
Inc
;; Author: Inge Wallin <[email protected]>
;; Toby Cubitt <[email protected]>
@@ -46,6 +46,11 @@
(eval-when-compile (require 'cl))
+(defmacro queue--when-generators (then)
+ "Evaluate THEN if `generator' library is available."
+ (declare (debug t))
+ (if (require 'generator nil 'noerror) then))
+
(defstruct (queue
;; A tagged list is the pre-defstruct representation.
@@ -144,6 +149,16 @@ order. The elements themselves are *not* copied."
(queue-tail queue) nil))
+(queue--when-generators
+ (iter-defun queue-iter (queue)
+ "Return a queue iterator object.
+
+Calling `iter-next' on this object will retrieve the next element
+from the queue. The queue itself is not modified."
+ (let ((list (queue-head queue)))
+ (while list (iter-yield (pop list))))))
+
+
(provide 'queue)