aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-03-21 02:37:54 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-03-21 02:37:54 +0100
commit9dc66f01d3116e663627c9e9f15a0de60035a246 (patch)
tree310e0da02e813c69002e15dbfe67c513e3febbfa
parentAdd find-min. (diff)
downloadcalp-9dc66f01d3116e663627c9e9f15a0de60035a246.tar.gz
calp-9dc66f01d3116e663627c9e9f15a0de60035a246.tar.xz
Started work on stream interleaving.
-rw-r--r--srfi/srfi-41/util.scm24
1 files changed, 24 insertions, 0 deletions
diff --git a/srfi/srfi-41/util.scm b/srfi/srfi-41/util.scm
new file mode 100644
index 00000000..ed378ea4
--- /dev/null
+++ b/srfi/srfi-41/util.scm
@@ -0,0 +1,24 @@
+(define-module (srfi srfi-41 util)
+ #:use-module (srfi srfi-1)
+ #:use-module (srfi srfi-41)
+ #:use-module (util) ; let*, find-min
+ #:export (stream-car+cdr interleave-streams))
+
+(define (stream-car+cdr stream)
+ (values (stream-car stream)
+ (stream-cdr stream)))
+
+;; Merges a number of totally ordered streams into a single
+;; totally ordered stream.
+;; ((≺, stream)) → (≺, stream)
+(define (interleave-streams < streams)
+ ;; Drop all empty streams
+ (let ((streams (remove stream-null? streams)))
+ ;; If all streams where empty, end the output stream
+ (if (null? streams)
+ stream-null
+ (let* ((min other (find-min < stream-car streams))
+ (m ms (stream-car+cdr min)))
+ (stream-cons m (interleave-streams < (cons ms other)))))))
+
+;;; Varför är allting så långsamt‽‽‽‽‽‽‽‽