diff --git a/Makefile b/Makefile index 86e7925..f3588a2 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ TARGET := $(shell guile -c "(display (%site-dir))") CCACHE := $(shell guile -c "(display(%site-ccache-dir))") -OBJ := csv.go +OBJ := csv.go transform.scm .PHONY: all clean install uninstall all: $(OBJ) diff --git a/csv/csv.scm b/csv/csv.scm index dab98a0..f59dbb2 100644 --- a/csv/csv.scm +++ b/csv/csv.scm @@ -1,7 +1,8 @@ ;; guile-csv -;; Copyright (C) 2008, 2012, 2013 +;; Copyright (C) 2008, 2012, 2013, 2021 ;; Andy Wingo ;; Nala Ginrut +;; Brady McDonough ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU Lesser General Public License as diff --git a/csv/transform.scm b/csv/transform.scm new file mode 100644 index 0000000..648d7ec --- /dev/null +++ b/csv/transform.scm @@ -0,0 +1,45 @@ +;; guile-csv transform +;; Copyright (C) 2021 +;; Brady McDonough + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU Lesser General Public License as +;; published by the Free Software Foundation; either version 3 of the +;; License, or (at your option) any later version. +;; +;; This program is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; Lesser General Public License for more details. +;; +;; You should have received a copy of the GNU Lesser General Public +;; License along with this program; if not, contact: +;; +;; Free Software Foundation, Inc. Voice: +1-617-542-5942 +;; 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 +;; Boston, MA 02110-1301, USA gnu@gnu.org + +(define-module (csv transform) + #:use-module (ice-9 receive) + #:use-module (sxml transform) + #:export (bubble-term) + #:reexport (pre-post-order)) + +(define (key-contains-defer str) + (lambda ($pair) (string-contains (symbol->string (car $pair)) str))) + +;; Generates a handler for pre-post-order. +;; EXAMPLE USAGE: +;; (pre-post-order $tree `(,(bubble-term rec "key-name") +;; (*default* . ,(lambda x x)))) +;; This will generate a rule which travels in *preorder* to each record named +;;'rec' and replace the rec symbol with the pair keyed as "key-name" +(define-syntax bubble-term + (syntax-rules () + ((_ record-sym $str) + `(record-sym *preorder* . + ,(lambda $record + (let* (($lst (cadr $record))) + (receive (head rest) + (partition (key-contains-defer $str) $lst) + (cons head (cons rest '())))))))))