utils: Move drop-first-last from rs and add list-sets->list-strings.

This commit is contained in:
Sergiu Ivanov 2020-03-02 23:37:37 +01:00
parent c0d36a34a7
commit b5f5eb55fc
3 changed files with 18 additions and 5 deletions

4
rs.rkt
View file

@ -110,10 +110,6 @@
;;; A shortcut to read-context-sequence. ;;; A shortcut to read-context-sequence.
(define-syntax-rule (read-ctx str) (read-context-sequence str)) (define-syntax-rule (read-ctx str) (read-context-sequence str))
;;; Removes the first and the last symbol of a given string.
(define (drop-first-last str)
(substring str 1 (- (string-length str) 1)))
;;; Converts a reaction to a triple of strings. ;;; Converts a reaction to a triple of strings.
(define/match (reaction->str-triple r) (define/match (reaction->str-triple r)
[((reaction r i p)) [((reaction r i p))

View file

@ -61,7 +61,10 @@
(check-equal? (hash-ref m1 'b) '(or b (not a))) (check-equal? (hash-ref m1 'b) '(or b (not a)))
(check-equal? (hash-ref m2 'b) '(or b (not a))) (check-equal? (hash-ref m2 'b) '(or b (not a)))
(check-equal? (hash-ref m3 'b) '(or b (not a)))) (check-equal? (hash-ref m3 'b) '(or b (not a))))
(check-equal? (read-symbol-list "a b c") '(a b c))) (check-equal? (read-symbol-list "a b c") '(a b c))
(check-equal? (drop-first-last "(a b)") "a b")
(check-equal? (list-sets->list-strings (list (set 'x 'y) (set 'z) (set) (set 't)))
'("y x" "z" "" "t")))
(test-case "Additional graph utilities" (test-case "Additional graph utilities"
(let* ([gr1 (directed-graph '((a b) (b c)))] (let* ([gr1 (directed-graph '((a b) (b c)))]

View file

@ -20,6 +20,8 @@
(listof (general-pair/c symbol? any/c)))] (listof (general-pair/c symbol? any/c)))]
[read-org-variable-mapping (-> string? variable-mapping?)] [read-org-variable-mapping (-> string? variable-mapping?)]
[read-symbol-list (-> string? (listof symbol?))] [read-symbol-list (-> string? (listof symbol?))]
[drop-first-last (-> string? string?)]
[list-sets->list-strings (-> (listof (set/c any/c)) (listof string?))]
[update-vertices/unweighted (-> graph? (-> any/c any/c) graph?)] [update-vertices/unweighted (-> graph? (-> any/c any/c) graph?)]
[update-graph (->* (graph?) [update-graph (->* (graph?)
(#:v-func (-> any/c any/c) (#:v-func (-> any/c any/c)
@ -224,6 +226,18 @@
(define (read-symbol-list str) (define (read-symbol-list str)
(string->any (string-append "(" str ")"))) (string->any (string-append "(" str ")")))
;;; Removes the first and the last symbol of a given string.
;;;
;;; Useful for removing the parentheses in string representations of
;;; lists.
(define (drop-first-last str)
(substring str 1 (- (string-length str) 1)))
;;; Converts a list of sets of symbols to a list of strings containing
;;; those symbols.
(define (list-sets->list-strings lst)
(map (compose drop-first-last any->string set->list) lst))
;;; ========================== ;;; ==========================
;;; Additional graph utilities ;;; Additional graph utilities