;;; chicanery extras --- extra stuff from ur old pal acdw ;;; Generalized map, for-each, ... ;; List versions are renamed `list-'. Un-prefixed versions work ;; with any (default) datatype. TODO: generalize? (define list-map scheme/map) (define list-for-each scheme/for-each) (define list-append scheme/append) (define (map proc . collections) (let ((first (car collections))) ; we only need to check the first one (cond ((list? first) (apply list-map proc collections)) ((vector? first) (apply vector-map proc collections)) ((string? first) (apply string-map proc collections)) (else (error "Bad datatype" first))))) (define (for-each proc . collections) (let ((first (car collections))) ; we only need to check the first one (cond ((list? first) (apply list-for-each proc collections)) ((vector? first) (apply vector-for-each proc collections)) ((string? first) (apply string-for-each proc collections)) (else (error "Bad datatype" first))))) (define (append . collections) (let ((first (car collections))) ; we only need to check the first one (cond ((list? first) (apply list-append collections)) ((vector? first) (apply vector-append collections)) ((string? first) (apply string-append collections)) ((bytevector? first) (apply bytevector-append collections)) (else (error "Bad datatype" first))))) ;;; Extended generic functions. ;; These functions already have a list- version defined, but no non-prefixed ;; one. So I'm fixing that. (define (ref collection k) (cond ((list? collection) (list-ref collection k)) ((vector? collection) (vector-ref collection k)) ((string? collection) (string-ref collection k)) ((bytevector? collection) (bytevector-u8-ref collection k)) (else (error "Bad datatype" collection)))) ;; I'm not going to generalize -copy! because I don't think it's a great idea, ;; really. (define (copy collection) (cond ((list? collection) (list-copy collection)) ((vector? collection) (vector-copy collection)) ((string? collection) (string-copy collection)) ((bytevector? collection) (bytevector-copy collection)) (else (error "Bad datatype" collection)))) ;;; Definitions that should be in scheme ;; This sections should be as small as possible (define (atom? x) (and (not (pair? x)) (not (null? x)))) (define read-port-chunk-size (make-parameter 512)) (define (%read-port port) (let ((chunk-size (read-port-chunk-size))) (let loop ((next (read-string chunk-size port)) (blank? #f) (acc '())) (cond ((or (eof-object? next) (and blank? (equal? next ""))) (apply string-append (reverse acc))) ((equal? next "") (loop (read-string chunk-size port) #t (cons next acc))) (else (loop (read-string chunk-size port) blank? (cons next acc))))))) (define read-port (case-lambda (() (%read-port (current-input-port))) ((p) (%read-port p)))) (define (defined? sym) (call-with-current-continuation (lambda (k) (with-exception-handler (lambda (e) (k #f)) (lambda () (eval sym (interaction-environment)) #t))))) (define (displayed x) (call-with-port (open-output-string) (lambda (port) (display x port) (get-output-string port)))) (define (written x) (call-with-port (open-output-string) (lambda (port) (write x port) (get-output-string port)))) (define (print . xs) (for-each display xs) (newline)) (define ->string displayed)