-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterators.lisp
executable file
·197 lines (157 loc) · 4.44 KB
/
iterators.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
;#!/usr/local/bin/sbcl --script
;;;; Hey, Emacs, this is a -*- Mode: Lisp; Syntax: Common-Lisp -*- file!
;;;;
;;;; Lisp is a programmable programming language.
;;;; -- John Foderaro
;;;;
;;;; Name: iterators.lisp
;;;;
;;;; Started: Sat Jun 30 20:19:29 2012
;;;; Modifications:
;;;;
;;;; Purpose:
;;;;
;;;;
;;;;
;;;; Calling Sequence:
;;;;
;;;;
;;;; Inputs:
;;;;
;;;; Outputs:
;;;;
;;;; Example:
;;;;
;;;; Notes: Create WITH- macros: WITH-LIST-ITERATOR, WITH-SET-ITERATOR, WITH-STRING-ITERATOR, WITH-VECTOR-ITERATOR
;;;;
;;;; Why aren't the MAKE- functions generic???
;;;;
(load "/Users/dsletten/lisp/packages/collections.lisp")
(load "/Users/dsletten/lisp/packages/test.lisp")
(defpackage :iterators
(:shadowing-import-from :collections :intersection :set :set-difference :subsetp :union)
(:use :common-lisp :collections :test)
(:export :has-next-p
:make-hash-table-iterator :make-list-iterator :make-set-iterator :make-string-iterator :make-vector-iterator
:next)
(:shadow :next)) ; Is this a good idea???
(in-package :iterators)
(defclass iterator ()
(index
(contents :initarg :contents)))
(defclass list-iterator (iterator) ())
(defgeneric has-next-p (iterator))
(defgeneric next (iterator))
(defun make-list-iterator (list)
(assert (listp list))
(make-instance 'list-iterator :contents list))
(defmethod initialize-instance :after ((i list-iterator) &key)
(with-slots (index contents) i
(setf index contents)))
(defmethod has-next-p ((i list-iterator))
(not (endp (slot-value i 'index))))
(defmethod next :around ((i iterator))
(if (has-next-p i)
(call-next-method)
nil))
(defmethod next ((i list-iterator))
(with-slots (index) i
(first index)))
(defmethod next :after ((i list-iterator))
(with-slots (index) i
(setf index (rest index))))
(defclass vector-iterator (iterator) ())
(defun make-vector-iterator (vector)
(assert (vectorp vector))
(make-instance 'vector-iterator :contents vector))
(defmethod initialize-instance :after ((i vector-iterator) &key)
(with-slots (index) i
(setf index 0)))
(defmethod has-next-p ((i vector-iterator))
(with-slots (index contents) i
(< index (length contents))))
(defmethod next ((i vector-iterator))
(with-slots (index contents) i
(aref contents index)))
(defmethod next :after ((i vector-iterator))
(with-slots (index contents) i
(incf index)))
(defclass string-iterator (vector-iterator) ())
(defun make-string-iterator (string)
(assert (stringp string))
(make-instance 'string-iterator :contents string))
(defmethod next ((i string-iterator))
(with-slots (index contents) i
(char contents index)))
;;;
;;; WITH-HASH-TABLE-ITERATOR...
;;;
(defclass hash-table-iterator (list-iterator)
((hash-table :initarg :hash-table)))
(defun make-hash-table-iterator (h)
(assert (hash-table-p h))
(make-instance 'hash-table-iterator :contents (hash-keys h) :hash-table h))
(defmethod next ((i hash-table-iterator))
(with-slots (index hash-table) i
(list (first index) (gethash (first index) hash-table))))
(defclass set-iterator (list-iterator) ())
(defun make-set-iterator (s)
(assert (typep s 'set))
(make-instance 'set-iterator :contents (elements s)))
;; (defvar *i* (make-hash-table-iterator {:a 1 :b 2 :c 3}))
;; (loop (unless (has-next-p *i*) (return)) (print (next *i*)))
;; (:C 3)
;; (:B 2)
;; (:A 1)
;; (setf *i* (make-list-iterator '(a b c d e)))
;; (loop (unless (has-next-p *i*) (return)) (print (next *i*)))
;; A
;; B
;; C
;; D
;; E
;; (setf *i* (make-vector-iterator [1 2 3 4 5 6]))
;; (loop (unless (has-next-p *i*) (return)) (print (next *i*)))
;; 1
;; 2
;; 3
;; 4
;; 5
;; 6
;; (setf *i* (make-string-iterator "Is this not pung?"))
;; (loop (unless (has-next-p *i*) (return)) (print (next *i*)))
;; #\I
;; #\s
;; #\Space
;; #\t
;; #\h
;; #\i
;; #\s
;; #\Space
;; #\n
;; #\o
;; #\t
;; #\Space
;; #\p
;; #\u
;; #\n
;; #\g
;; #\?
;; (defvar *h* {:a 1 :b 2 :c 3})
;; (setf *i* (make-hash-table-iterator *h*))
;; (loop (unless (has-next-p *i*) (return)) (destructuring-bind (key val) (next *i*) (incf (gethash key *h*))))
;; (setf *i* (make-hash-table-iterator *h*))
;; (loop (unless (has-next-p *i*) (return)) (print (next *i*)))
;; (:C 4)
;; (:B 3)
;; (:A 2)
;; (setf *i* (make-set-iterator #{1 2 :k 3 :f "Bob" 4 5}))
;; (loop (unless (has-next-p *i*) (return)) (print (next *i*)))
;; 5
;; 4
;; "Bob"
;; :F
;; 3
;; :K
;; 2
;; 1