#|[ Author: Juergen Lorenz ju (at) jugilo (dot) de Copyright (c) 2015-2019, Juergen Lorenz All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |# (module functional-vectors (functional-vectors fvector fvector? fvector-ref fvector-tail fvector-length fvector-data) (import scheme (only bindings bind-seq-db) (only checks <<) (only (chicken base) print error gensym case-lambda call/cc define-values) (only (chicken condition) condition-case)) (define-values (fvector fvector?) (let ((type (gensym 'fvector))) (values (lambda args (let ((vec (apply vector args))) (case-lambda ((k) (vector-ref vec k)) (() type)))) (lambda (xpr) (and (procedure? xpr) (condition-case (eq? (xpr) type) ((exn arity) #f))))))) (define (0<= k) (and (integer? k) (<= 0 k))) (define (fvector-ref fv k) (let ((fv (<< fv fvector?)) (k (<< k 0<=))) (fv k))) (define (fvector-tail fv k) (let ((fv (<< fv fvector?)) (k (<< k 0<=))) (let ((vec (call/cc (lambda (out) (let loop ((n k) (result '())) (loop (+ n 1) (cons (condition-case (fv n) ((exn bounds) (out (apply vector (reverse result))))) result))))))) (case-lambda ((n) (vector-ref vec n)) (() (fv)))))) (define (fvector-length fv) (let ((fv (<< fv fvector?))) (call/cc (lambda (out) (let loop ((k 0) (result 0)) (condition-case (fvector-ref fv k) ((exn bounds) (out result))) (loop (+ 1 k) (+ 1 result))))))) (define (fvector-data fv) (let ((fv (<< fv fvector?))) (let loop ((k (- (fvector-length fv) 1)) (result '())) (if (negative? k) result (let ((val (fv k))) (if (fvector? val) (loop (- k 1) (cons (fvector-data val) result)) (loop (- k 1) (cons val result)))))))) (define (symbol-dispatcher alist) (case-lambda (() (map car alist)) ((sym) (let ((pair (assq sym alist))) (if pair (for-each print (cdr pair)) (error "Not in list" sym (map car alist))))))) ;;; make functional vectors accessible to pattern matching ;;; ------------------------------------------------------ (bind-seq-db fvector? #:ref fvector-ref #:tail fvector-tail) ;;; (functional-vectors sym ..) ;;; --------------------------- ;;; documentation procedure (define functional-vectors (symbol-dispatcher '( (functional-vectors procedure: (functional-vectors sym ..) "documentation procedure") (fvector procedure: (fvector . args) "constructor") (fvector? procedure: (fvector? xpr) "predicate") (fvector-ref procedure: (fvector-ref fv k) "accessor") (fvector-tail procedure: (fvector-tail fv k) "accessor") (fvector-data procedure: (fvector-data fv) "transforms a (possibly nested) fvector" "into a (possibly nested) list") ))) ) ; functional-vectors ;(import functional-vectors bindings simple-tests) ; ;(ppp (fvector-data (fvector)) ; (fvector-data (fvector 0 1 2)) ; (fvector-data (fvector 0 (fvector 1 2))) ; (fvector-data (fvector 0 (fvector 1 (fvector 2)))) ; (fvector-data (fvector foo: bar: (fvector 1 2 3))) ; (fvector-ref (fvector 0 1 2) 1) ; (bind (a (b c)) (fvector 0 (fvector 1 2)) (list a b c)) ; )