;;;; streams-utils.scm ;;;; Kon Lovett, Apr '09 ; Copyright (C) 2007 by Philip L. Bewig of Saint Louis, Missouri, USA. All rights ; reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of ; this software and associated documentation files (the "Software"), to deal in the Software ; without restriction, including without limitation the rights to use, copy, modify, merge, ; publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to ; whom the Software is furnished to do so, subject to the following conditions: The above ; copyright notice and this permission notice shall be included in all copies or substantial ; portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS ; FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT ; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF ; CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR ; THE USE OR OTHER DEALINGS IN THE SOFTWARE. (module streams-utils (;export stream-intersperse stream-permutations file->stream stream-split stream-unique stream-fold-one stream-member stream-merge stream-partition stream-finds stream-find stream-remove stream-every stream-any stream-and stream-or stream-fold-right stream-fold-right-one stream-assoc stream-equal? stream-quick-sort stream-insertion-sort stream-merge-sort stream-maximum stream-minimum) (import scheme chicken (only data-structures complement right-section) streams (only type-errors error-procedure error-string error-cardinal-integer)) (require-library streams type-errors) (include "chicken-primitive-object-inlines") (include "streams-inlines") (include "inline-type-checks") ;;; (define-stream (stream-intersperse yy x) (%check-stream 'stream-intersperse yy 'stream) (stream-match yy (() (stream (stream x))) ((y . ys) (stream-append (stream (stream-cons x yy)) (stream-map (lambda (z) (stream-cons y z)) (stream-intersperse ys x))) ) ) ) (define-stream (stream-permutations xs) (%check-stream 'stream-permutations xs 'stream) (if (stream-null? xs) (stream (stream)) (stream-concat (stream-map (right-section stream-intersperse (stream-car xs)) (stream-permutations (stream-cdr xs)))) ) ) (define-stream (file->stream filename #!optional (reader read-char)) (%check-string 'file->streams filename 'filename) (%check-procedure 'file->streams reader 'reader) (let ((port (open-input-file filename))) (stream-let loop ((item (reader port))) (if (eof-object? item) (begin (close-input-port port) stream-null) (stream-cons item (loop (reader port))) ) ) ) ) (define (stream-split count strm) (%check-stream 'stream-split strm 'stream) (%check-cardinal-integer 'stream-split count 'count) (values (stream-take count strm) (stream-drop count strm))) (define-stream (stream-unique eql? strm) (%check-stream 'stream-unique strm 'stream) (%check-procedure 'stream-unique eql? 'equivalence) (stream-let loop ((strm strm)) (if (stream-null? strm) stream-null (stream-cons (stream-car strm) (loop (stream-drop-while (lambda (x) (eql? (stream-car strm) x)) strm))) ) ) ) (define (stream-fold-one func strm) (%check-stream 'stream-fold-one strm 'stream) (%check-procedure 'stream-fold-one func 'function) (stream-fold func (stream-car strm) (stream-cdr strm)) ) (define-stream (stream-member eql? item strm) (%check-stream 'stream-member strm 'stream) (%check-procedure 'stream-member eql? 'equivalence) (stream-let loop ((strm strm)) (cond ((stream-null? strm) #f) ((eql? item (stream-car strm)) strm) (else (loop (stream-cdr strm)) ) ) ) ) (define-stream (stream-merge lt? . strms) (define-stream (stream-merge$ xx yy) (stream-match xx (() yy ) ((x . xs) (stream-match yy (() xx ) ((y . ys) (if (lt? y x) (stream-cons y (stream-merge$ xx ys)) (stream-cons x (stream-merge$ xs yy))))) ) ) ) (%check-procedure 'stream-merge lt? 'less-than) (%check-streams 'stream-merge strms 'stream) (stream-let loop ((strms strms)) (cond ((null? strms) stream-null) ((null? (%cdr strms)) (%car strms)) (else (stream-merge$ (%car strms) (apply stream-merge lt? (%cdr strms))) ) ) ) ) (define (stream-partition pred? strm) (%check-stream 'stream-partition strm 'stream) (%check-procedure 'stream-partition pred? 'predicate) (stream-unfolds (lambda (s) (if (stream-null? s) (values s '() '()) (let ((a (stream-car s)) (d (stream-cdr s))) (if (pred? a) (values d (list a) #f) (values d #f (list a)) ) ) ) ) strm) ) (define-stream (stream-finds eql? item strm) (%check-stream 'stream-finds strm 'stream) (%check-procedure 'stream-finds eql? 'equivalence) (stream-of (%car x) (x in (stream-zip (stream-from 0) strm)) (eql? item (%cadr x))) ) (define (stream-find eql? item strm) (%check-stream 'stream-find strm 'stream) (%check-procedure 'stream-find eql? 'equivalence) (stream-car (stream-append (stream-finds eql? item strm) (stream #f))) ) (define-stream (stream-remove pred? strm) (%check-stream 'stream-remove strm 'stream) (%check-procedure 'stream-remove pred? 'predicate) (stream-filter (complement pred?) strm) ) (define (stream-every pred? strm) (%check-stream 'stream-every strm 'stream) (%check-procedure 'stream-every pred? 'predicate) (let loop ((strm strm)) (cond ((stream-null? strm) #t) ((not (pred? (stream-car strm))) #f) (else (loop (stream-cdr strm)) ) ) ) ) (define (stream-any pred? strm) (%check-stream 'stream-any strm 'stream) (%check-procedure 'stream-any pred? 'predicate) (let loop ((strm strm)) (cond ((stream-null? strm) #f) ((pred? (stream-car strm)) #t) (else (loop (stream-cdr strm)) ) ) ) ) (define (stream-and strm) (%check-stream 'stream-and strm 'stream) (let loop ((strm strm)) (cond ((stream-null? strm) #t) ((not (stream-car strm)) #f) (else (loop (stream-cdr strm)) ) ) ) ) (define (stream-or strm) (%check-stream 'stream-or strm 'stream) (let loop ((strm strm)) (cond ((stream-null? strm) #f) ((stream-car strm) #t) (else (loop (stream-cdr strm)) ) ) ) ) (define (stream-fold-right func base strm) (%check-stream 'stream-fold-right strm 'stream) (%check-procedure 'stream-fold-right func 'function) (let loop ((strm strm)) (if (stream-null? strm) base (func (stream-car strm) (loop (stream-cdr strm))) ) ) ) (define (stream-fold-right-one func strm) (%check-stream 'stream-fold-right-one strm 'stream) (%check-procedure 'stream-fold-right-one func 'function) (let loop ((strm strm)) (stream-match strm ((x) x ) ((x . xs) (func x (loop xs)) ) ) ) ) (define (stream-assoc key dict #!optional (eql? equal?)) (%check-stream 'stream-assoc dict 'stream) (%check-procedure 'stream-assoc eql? 'equivalence) (let loop ((dict dict)) (cond ((stream-null? dict) #f) ((eql? key (%car (stream-car dict))) (stream-car dict) ) (else (loop (stream-cdr dict)) ) ) ) ) (define (stream-equal? eql? xs ys) (%check-stream 'stream-equal? xs 'stream1) (%check-stream 'stream-equal? ys 'stream2) (let loop ((xs xs) (ys ys)) (cond ((and (stream-null? xs) (stream-null? ys)) #t) ((or (stream-null? xs) (stream-null? ys)) #f) ((not (eql? (stream-car xs) (stream-car ys))) #f) (else (loop (stream-cdr xs) (stream-cdr ys)) ) ) ) ) (define-stream (stream-quick-sort lt? strm) (%check-stream 'stream-quick-sort strm 'stream) (%check-procedure 'stream-quick-sort lt? 'less-than) (let loop ((strm strm)) (if (stream-null? strm) stream-null (let ((x (stream-car strm)) (xs (stream-cdr strm))) (stream-append (loop (stream-filter (lambda (u) (lt? u x)) xs)) (stream x) (loop (stream-filter (lambda (u) (not (lt? u x))) xs))) ) ) ) ) (define-stream (stream-insertion-sort lt? strm) (define-stream (insert$ strm x) (stream-match strm (() (stream x) ) ((y . ys) (if (lt? y x) (stream-cons y (insert$ ys x)) (stream-cons x strm) ) ) ) ) (%check-stream 'stream-insertion-sort strm 'stream) (%check-procedure 'stream-insertion-sort lt? 'less-than) (stream-fold insert$ stream-null strm) ) (define-stream (stream-merge-sort lt? strm) (%check-stream 'stream-merge-sort strm 'stream) (%check-procedure 'stream-merge-sort lt? 'less-than) (let loop ((strm strm)) (let ((n (quotient (stream-length strm) 2))) (if (zero? n) strm (stream-merge lt? (loop (stream-take n strm)) (loop (stream-drop n strm))) ) ) ) ) (define (stream-maximum lt? strm) (%check-stream 'stream-maximum strm 'stream) (%check-procedure 'stream-maximum lt? 'less-than) (stream-fold-one (lambda (x y) (if (lt? x y) y x)) strm) ) (define (stream-minimum lt? strm) (%check-stream 'stream-minimum strm 'stream) (%check-procedure 'stream-minimum lt? 'less-than) (stream-fold-one (lambda (x y) (if (lt? x y) x y)) strm) ) ) ;module streams-utils