#| Copyright 2024 Peter McGoron | | Licensed under the Apache License, Version 2.0 (the "License"); | you may not use this file except in compliance with the License. | You may obtain a copy of the License at | | http://www.apache.org/licenses/LICENSE-2.0 | | Unless required by applicable law or agreed to in writing, software | distributed under the License is distributed on an "AS IS" BASIS, | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | See the License for the specific language governing permissions and | limitations under the License. |# (define-record-type (make-iterator start? end? advance ref to-index comparison private) iterator? (start? get-start-predicate) (end? get-end-predicate) (advance get-advance) (ref get-ref) (to-index get-to-index-procedure) (comparison get-comparison-procedure) (private iterator-get-private)) ;;; Define a function that invokes a field of the iterator on the data ;;; object inside the iterator and any other arguments supplied to the ;;; function. (define-syntax define-invoke-field-of-iterator (syntax-rules () ((define-invoker name field-accessor emsg args ...) (define (name iterator args ...) (let ((proc (field-accessor iterator))) (if (not proc) (error emsg iterator) (proc args ...))))))) (define-invoke-field-of-iterator iterator-at-start? get-start-predicate "no start predicate") (define-invoke-field-of-iterator iterator-at-end? get-end-predicate "no end predicate") (define-invoke-field-of-iterator iterator-advance get-advance "no procedure to move iterator" spaces) (define-invoke-field-of-iterator iterator-ref get-ref "no procedure to access value at iterator") (define-invoke-field-of-iterator iterator->index get-to-index-procedure "no procedure to convert iterator->index") (define-syntax define-comparison (syntax-rules () ((_ name comparison-function) (define (name itr1 . itr-rest) ((get-comparison-procedure itr1) comparison-function itr-rest))))) (define-comparison iterator=? =?) (define-comparison iterator<=? <=?) (define-comparison iterator>=? >=?) (define-comparison iterator? >?)