From 3400af94611839efd710d191f983be6d05df86da Mon Sep 17 00:00:00 2001 From: jmarshall Date: Fri, 15 Jan 2016 20:50:27 -0800 Subject: [PATCH] Add STREAM-LAST and STREAM-TRUNCATE. Fixed a typo. --- src/runtime/runtime.pkg | 2 ++ src/runtime/stream.scm | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/runtime/runtime.pkg b/src/runtime/runtime.pkg index 4a9bdf35a..10a558c8c 100644 --- a/src/runtime/runtime.pkg +++ b/src/runtime/runtime.pkg @@ -4606,6 +4606,7 @@ USA. stream-first stream-for-each stream-head + stream-last stream-last-pair stream-length stream-map @@ -4614,6 +4615,7 @@ USA. stream-ref stream-rest stream-tail + stream-truncate stream-write tail the-empty-stream) diff --git a/src/runtime/stream.scm b/src/runtime/stream.scm index 31da2be30..6c49ebc35 100644 --- a/src/runtime/stream.scm +++ b/src/runtime/stream.scm @@ -90,6 +90,9 @@ USA. (loop (force (cdr stream)) (- index 1))) stream))) +(define (stream-last stream) + (stream-car (stream-last-pair stream))) + (define (stream-last-pair stream) (if (not (stream-pair? stream)) (if (null? stream) @@ -100,7 +103,7 @@ USA. (if (stream-pair? next) (loop next) (begin - (if (not (null? stream)) + (if (not (null? next)) (error:illegal-stream-element stream 'STREAM-LAST-PAIR 0)) stream))))) @@ -223,6 +226,17 @@ USA. (error:illegal-stream-element stream 'STREAM-FILTER 1)) '()))) +(define (stream-truncate stream predicate) + (if (stream-pair? stream) + (if (predicate (head stream)) + the-empty-stream + (cons-stream (head stream) + (stream-truncate (tail stream) predicate))) + (begin + (if (not (null? stream)) + (error:illegal-stream-element stream 'STREAM-TRUNCATE 1)) + '()))) + (define (stream-write stream #!optional port) (let ((port (if (default-object? port) -- 2.25.1