cl-scopes/util/iter.lisp

30 lines
728 B
Common Lisp

;;;; cl-scopes/util/iter
;;;; iterators, queues, and other sequentially accessible stuff
;;;; producing items (objects) like: numbers, strings, symbols, lists, ...
(defpackage :scopes/util/iter
(:use :common-lisp)
(:export #:next #:value
#:list-iterator))
(in-package :scopes/util/iter)
;;;; iterators
(defgeneric next (it))
(defgeneric value (it))
(defclass list-iterator ()
((data :reader data :initarg :data :initform nil)
(cur :accessor cur)))
(defmethod initialize-instance :after ((it list-iterator) &key &allow-other-keys)
(setf (cur it) (cons nil (data it))))
(defmethod next ((it list-iterator))
(pop (cur it))
(null (cur it)))
(defmethod value ((it list-iterator))
(car (cur it)))