From 17ffa3eb81ae4ca1e4864aeeaba6f7be69cd0f4c Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Tue, 14 Jul 2020 23:55:55 +0200 Subject: [PATCH] functions: Add read-org-sbfs. --- functions.rkt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/functions.rkt b/functions.rkt index cbe21bb..7a01699 100644 --- a/functions.rkt +++ b/functions.rkt @@ -41,7 +41,8 @@ [tbf-tabulate (-> tbf? (listof (listof (or/c 0 1))))] [tbf-tabulate*/boolean (-> (listof tbf?) (listof (listof boolean?)))] [sbf (-> (vectorof number?) tbf?)] - [list->sbf (-> (listof number?) sbf?)]) + [list->sbf (-> (listof number?) sbf?)] + [read-org-sbfs (->* (string?) (#:headers boolean?) (listof sbf?))]) ;; Predicates (contract-out [sbf? (-> any/c boolean?)])) @@ -391,3 +392,18 @@ (module+ test (check-equal? (list->sbf '(1 -1)) (tbf '#(1 -1) 0))) + +;;; Reads a list of SBF from an Org-mode string containing a sexp, +;;; containing a list of lists of numbers. If headers is #t, drops +;;; the first list, supposing that it contains the headers of the +;;; table. +;;; +;;; The input is typically what read-org-sexp reads. +(define (read-org-sbfs str #:headers [headers #f]) + (define sexp (read-org-sexp str)) + (define sexp-clean (cond [headers (cdr sexp)] [else sexp])) + (map list->sbf sexp-clean)) + +(module+ test + (check-equal? (read-org-sbfs "((1 1) (1 -1))") + (list (tbf '#(1 1) 0) (tbf '#(1 -1) 0))))