diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2020-04-06 22:42:57 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2020-04-06 22:42:57 +0200 |
commit | 19efa7fe7c52807618a7800722ec34a45471fcb1 (patch) | |
tree | 72e0342531bf7183d331e8251f64dba377e41a0d /module/util.scm | |
parent | Add multiple new text formatting procedures. (diff) | |
download | calp-19efa7fe7c52807618a7800722ec34a45471fcb1.tar.gz calp-19efa7fe7c52807618a7800722ec34a45471fcb1.tar.xz |
Add some utility functions.
Diffstat (limited to '')
-rw-r--r-- | module/util.scm | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/module/util.scm b/module/util.scm index d4c318a3..dbbc1bf7 100644 --- a/module/util.scm +++ b/module/util.scm @@ -34,6 +34,15 @@ +;; NOTE +;; Instead of returning the empty list a better default value +;; for when and unless would be the identity element for the +;; current context. +;; So (string-append (when #f ...)) would expand into +;; (string-append (if #f ... "")). +;; This however requires type interferance, which i don't +;; *currently* have. + (define-syntax-rule (when pred body ...) (if pred (begin body ...) '())) @@ -405,6 +414,32 @@ (hash-set! h key (cons value (hash-ref h key '()))))) (hash-map->list list h))) +;; Returns the cross product between l1 and l2. +;; each element is a cons cell. +(define-public (cross-product l1 l2) + (concatenate + (map (lambda (a) + (map (lambda (b) (cons a b)) + l2)) + l1))) + +;; Given an arbitary tree, do a pre-order traversal, appending all strings. +;; non-strings allso allowed, converted to strings and also appended. +(define-public (string-flatten tree) + (cond [(string? tree) tree] + [(list? tree) (string-concatenate (map string-flatten tree))] + [else (format #f "~a" tree)])) + +(define-public (intersperce item list) + (let loop ((flipflop #f) + (rem list)) + (if (null? rem) + '() + (if flipflop + (cons item (loop (not flipflop) rem)) + (cons (car rem) (loop (not flipflop) (cdr rem))) + )))) + (define-macro (use-modules* . forms) `(use-modules ,@(concatenate |