From fb7701869d3b2b561799bd1f160e333eba89da6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Wed, 22 Mar 2023 10:13:22 +0100 Subject: [PATCH] Row normalization. --- table.scm | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/table.scm b/table.scm index 5b3dc16..8a88112 100644 --- a/table.scm +++ b/table.scm @@ -43,10 +43,10 @@ (define (string->rows str) (string-split str "\n" #t)) - ;; Creates procedure that ensures a row has given number of elements + ;; Creates procedure that ensures a list has given number of elements ;; filling the missing elements with given filler (defaults to empty ;; string). - (define ((make-row-extender ds . ofl) row) + (define ((make-list-extender ds . ofl) row) (let ((fl (if (null? ofl) "" (car ofl)))) (let ((rs (length row))) (if (< rs ds) @@ -62,7 +62,7 @@ ;; number of elements using empty strings as filler. (define (table-rectangularize tbl) (let ((mrl (apply max (map length tbl)))) - (map (make-row-extender mrl) tbl))) + (map (make-list-extender mrl) tbl))) ;; Accepts list of lists of anything and returns a list of lists of ;; strings. @@ -80,6 +80,12 @@ (map string->rows r)) tbl)) + ;; Accepts a list of cells which are list of strings and returns a + ;; new list with all cells having the same number of text lines. + (define (table-normalize-row row) + (let ((ml (apply max (map length row)))) + (map (make-list-extender ml) row))) + ;; Ensures the table is rectangular and each cell is a list of strings. (define (table-prepare tbl) (table-prepare-cells @@ -96,11 +102,11 @@ (test-equal? string->rows (string->rows "asdf") '("asdf")) (test-equal? string->rows (string->rows "asdf\nqwer") '("asdf" "qwer")) (test-equal? string->rows (string->rows "\nasdf\nqwer") '("" "asdf" "qwer")) - (test-equal? make-row-extender - ((make-row-extender 5) '("test")) + (test-equal? make-list-extender + ((make-list-extender 5) '("test")) '("test" "" "" "" "")) - (test-equal? make-row-extender - ((make-row-extender 5 "x") '("test")) + (test-equal? make-list-extender + ((make-list-extender 5 "x") '("test")) '("test" "x" "x" "x" "x")) (test-equal? table-rectangularize (table-rectangularize '(("x" "y" "z") ("a" "b") ("1" "2" "3" "4"))) @@ -111,6 +117,9 @@ (test-equal? table-prepare-cells (table-prepare-cells '(("x" "y" "z" "") ("a" "b" "" "") ("1" "2" "3" "4"))) '((("x") ("y") ("z") ("")) (("a") ("b") ("") ("")) (("1") ("2") ("3") ("4")))) + (test-equal? table-normalize-row + (table-normalize-row '(("") ("a" "b"))) + '(("" "") ("a" "b"))) )) )