Row normalization.
This commit is contained in:
parent
44ae1a2339
commit
fb7701869d
1 changed files with 16 additions and 7 deletions
23
table.scm
23
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")))
|
||||
))
|
||||
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue