summaryrefslogtreecommitdiff
path: root/haunt
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2015-10-14 10:44:00 -0400
committerDavid Thompson <dthompson2@worcester.edu>2015-10-14 10:50:03 -0400
commit397bc485d4985b284c3302027c2d9e82353162b7 (patch)
treeed97cc48309d6ecaf93230e22791da49c250c2fd /haunt
parent32883938eb2faa135f268cfba851c6c6e3e6e478 (diff)
utils: Clean up and add tests.
* haunt/utils.scm (flat-map, string-split-at): Add docstring. (file-name-components): Adjust slightly to handle "/". (join-file-name-components): Use prefix string join grammar. (absolute-file-name): Add docstring. * test-env.in: New file. * tests/utils.scm: New file. * Makefile.am (TESTS, TEST_EXTENSIONS, SCM_LOG_COMPILER, AM_SCM_LOG_FLAGS): New variables. * configure.ac: Add test-env pre-processed file. * build-aux/test-driver: New file.
Diffstat (limited to 'haunt')
-rw-r--r--haunt/utils.scm22
1 files changed, 18 insertions, 4 deletions
diff --git a/haunt/utils.scm b/haunt/utils.scm
index 29e6ef2..764d6cb 100644
--- a/haunt/utils.scm
+++ b/haunt/utils.scm
@@ -57,9 +57,18 @@ flattened."
lst)))
(define (flat-map proc . lsts)
+ "Apply PROC to each element of each list in LSTS and return a new
+list in which nested lists are concatenated into the result.
+
+For example, the list (1 2 (3)) would be flattened to (1 2 3)."
(flatten (apply map proc lsts) 1))
(define (string-split-at str char-pred)
+ "Split STR at the first character that matches CHAR-PRED and return
+a list of one or two strings. Two strings are returned if the string
+was able to be split, with the character matching CHAR-PRED removed.
+A list containing only STR is returned if CHAR-PRED does not match any
+charcter."
(let ((i (string-index str char-pred)))
(if i
(list (string-take str i)
@@ -68,15 +77,20 @@ flattened."
(define (file-name-components file-name)
"Split FILE-NAME into the components delimited by '/'."
- (if (string-null? file-name)
- '()
- (string-split file-name #\/)))
+ (match file-name
+ ("" '())
+ ("/" '(""))
+ (_ (remove string-null? (string-split file-name #\/)))))
(define (join-file-name-components components)
"Join COMPONENTS into a file name string."
- (string-join components "/"))
+ (string-join components "/" 'prefix))
(define (absolute-file-name file-name)
+ "Return a an absolute file name string relative to the current
+working directory for FILE-NAME, a relative file name string. If
+FILE-NAME happens to already be absolute, FILE-NAME is returned
+as-is."
(if (absolute-file-name? file-name)
file-name
(string-append (getcwd) "/" file-name)))