30 lines
732 B
Common Lisp
30 lines
732 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 #:current #:next
|
|
#:list-iterator))
|
|
|
|
(in-package :scopes/util/iter)
|
|
|
|
;;;; iterators
|
|
|
|
(defgeneric next (it))
|
|
(defgeneric current (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))
|
|
(current it))
|
|
|
|
(defmethod current ((it list-iterator))
|
|
(car (cur it)))
|