|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (srfi srfi-171 meta) |
|
#:use-module (srfi srfi-9) |
|
#:use-module ((rnrs bytevectors) #:select (bytevector-length bytevector-u8-ref)) |
|
#:export (reduced reduced? |
|
unreduce |
|
ensure-reduced |
|
preserving-reduced |
|
|
|
list-reduce |
|
vector-reduce |
|
string-reduce |
|
bytevector-u8-reduce |
|
port-reduce |
|
generator-reduce)) |
|
|
|
|
|
|
|
(define-record-type <reduced> |
|
(reduced val) |
|
reduced? |
|
(val unreduce)) |
|
|
|
(define (ensure-reduced x) |
|
"Ensure that @var{x} is reduced" |
|
(if (reduced? x) |
|
x |
|
(reduced x))) |
|
|
|
|
|
|
|
|
|
|
|
(define (preserving-reduced reducer) |
|
(lambda (a b) |
|
(let ((return (reducer a b))) |
|
(if (reduced? return) |
|
(reduced return) |
|
return)))) |
|
|
|
|
|
(define (list-reduce f identity lst) |
|
(if (null? lst) |
|
identity |
|
(let ((v (f identity (car lst)))) |
|
(if (reduced? v) |
|
(unreduce v) |
|
(list-reduce f v (cdr lst)))))) |
|
|
|
(define (vector-reduce f identity vec) |
|
(let ((len (vector-length vec))) |
|
(let loop ((i 0) (acc identity)) |
|
(if (= i len) |
|
acc |
|
(let ((acc (f acc (vector-ref vec i)))) |
|
(if (reduced? acc) |
|
(unreduce acc) |
|
(loop (+ i 1) acc))))))) |
|
|
|
(define (string-reduce f identity str) |
|
(let ((len (string-length str))) |
|
(let loop ((i 0) (acc identity)) |
|
(if (= i len) |
|
acc |
|
(let ((acc (f acc (string-ref str i)))) |
|
(if (reduced? acc) |
|
(unreduce acc) |
|
(loop (+ i 1) acc))))))) |
|
|
|
(define (bytevector-u8-reduce f identity vec) |
|
(let ((len (bytevector-length vec))) |
|
(let loop ((i 0) (acc identity)) |
|
(if (= i len) |
|
acc |
|
(let ((acc (f acc (bytevector-u8-ref vec i)))) |
|
(if (reduced? acc) |
|
(unreduce acc) |
|
(loop (+ i 1) acc))))))) |
|
|
|
(define (port-reduce f identity reader port) |
|
(let loop ((val (reader port)) (acc identity)) |
|
(if (eof-object? val) |
|
acc |
|
(let ((acc (f acc val))) |
|
(if (reduced? acc) |
|
(unreduce acc) |
|
(loop (reader port) acc)))))) |
|
|
|
(define (generator-reduce f identity gen) |
|
(let loop ((val (gen)) (acc identity)) |
|
(if (eof-object? val) |
|
acc |
|
(let ((acc (f acc val))) |
|
(if (reduced? acc) |
|
(unreduce acc) |
|
(loop (gen) acc)))))) |
|
|
|
|