Merge successive strings #12

Closed
opened 5 years ago by sanchom · 6 comments
sanchom commented 5 years ago (Migrated from github.com)

Would this function be an appropriate addition to this package?

https://github.com/sanchom/sanchom.github.io/blob/master-source/util.rkt

(define/contract (merge-successive-strings elements)
  (txexpr-elements? . -> . txexpr-elements?)
  (define (conditional-merge element current-list)
    (if (and (not (empty? current-list))
             (string? (first current-list))
             (string? element))
        (cons (string-append (first current-list) element) (drop current-list 1))
        (cons element current-list)))
  (reverse (foldl conditional-merge '() elements)))

(module+ test
  (check-equal? (merge-successive-strings '()) '())
  (check-equal? (merge-successive-strings '("a")) '("a"))
  (check-equal? (merge-successive-strings '(a)) '(a))
  (check-equal? (merge-successive-strings '(a "b")) '(a "b"))
  (check-equal? (merge-successive-strings '("a" "b")) '("ab"))
  (check-equal? (merge-successive-strings '("a" a "b")) '("a" a "b"))
  (check-equal? (merge-successive-strings '("a" "b" " " "c")) '("ab c"))
  (check-equal? (merge-successive-strings '("a" "b" a " " b "c" c d "e" " f" g)) '("ab" a " " b "c" c d "e f" g))
)
Would this function be an appropriate addition to this package? https://github.com/sanchom/sanchom.github.io/blob/master-source/util.rkt ``` (define/contract (merge-successive-strings elements) (txexpr-elements? . -> . txexpr-elements?) (define (conditional-merge element current-list) (if (and (not (empty? current-list)) (string? (first current-list)) (string? element)) (cons (string-append (first current-list) element) (drop current-list 1)) (cons element current-list))) (reverse (foldl conditional-merge '() elements))) (module+ test (check-equal? (merge-successive-strings '()) '()) (check-equal? (merge-successive-strings '("a")) '("a")) (check-equal? (merge-successive-strings '(a)) '(a)) (check-equal? (merge-successive-strings '(a "b")) '(a "b")) (check-equal? (merge-successive-strings '("a" "b")) '("ab")) (check-equal? (merge-successive-strings '("a" a "b")) '("a" a "b")) (check-equal? (merge-successive-strings '("a" "b" " " "c")) '("ab c")) (check-equal? (merge-successive-strings '("a" "b" a " " b "c" c d "e" " f" g)) '("ab" a " " b "c" c d "e f" g)) ) ```
mbutterick commented 5 years ago (Migrated from github.com)
  1. Under what circumstances would one need to merge-successive-strings?

  2. I don’t think I understand what it claims to do:

> (merge-successive-strings '((p "a" "b") (div "c" "d")))
'((p "a" "b") (div "c" "d")) ; ???
1. Under what circumstances would one need to `merge-successive-strings`? 2. I don’t think I understand what it claims to do: ```racket > (merge-successive-strings '((p "a" "b") (div "c" "d"))) '((p "a" "b") (div "c" "d")) ; ??? ```
AlexKnauth commented 5 years ago (Migrated from github.com)

Oh, it's a shallow list traversal instead of a deep xexpr traversal.

Oh, it's a shallow list traversal instead of a deep xexpr traversal.
sanchom commented 5 years ago (Migrated from github.com)

I've needed to use it several times when programatically building up the
elements of a txexpr, several of which are strings, but some of which are
nested txexprs (like an em). In order for later processing to have an easy
time, it is helpful to put all the adjacent strings together into single
strings.

On Tue., Jun. 18, 2019, 11:49 Alex Knauth, notifications@github.com wrote:

Oh, it's a shallow list traversal instead of a deep xexpr traversal.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/mbutterick/txexpr/issues/12?email_source=notifications&email_token=AAFVL2ND6NYWXZMS2P3OXSDP3EU3VA5CNFSM4HYDVEG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODX7TIUA#issuecomment-503264336,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAFVL2LEHAXXGCFRWKUJRPLP3EU3VANCNFSM4HYDVEGQ
.

I've needed to use it several times when programatically building up the elements of a txexpr, several of which are strings, but some of which are nested txexprs (like an em). In order for later processing to have an easy time, it is helpful to put all the adjacent strings together into single strings. On Tue., Jun. 18, 2019, 11:49 Alex Knauth, <notifications@github.com> wrote: > Oh, it's a shallow list traversal instead of a deep xexpr traversal. > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > <https://github.com/mbutterick/txexpr/issues/12?email_source=notifications&email_token=AAFVL2ND6NYWXZMS2P3OXSDP3EU3VA5CNFSM4HYDVEG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODX7TIUA#issuecomment-503264336>, > or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAFVL2LEHAXXGCFRWKUJRPLP3EU3VANCNFSM4HYDVEGQ> > . >
mbutterick commented 5 years ago (Migrated from github.com)

I think not, because it doesn’t specially pertain to txexprs. Yes, this is one of many functions that operate on strings and lists that are conceivably useful with txexprs, though the txexpr library cannot hope to warehouse them all. Why not include it with your citation package?

I think not, because it doesn’t specially pertain to txexprs. Yes, this is one of many functions that operate on strings and lists that are conceivably useful with txexprs, though the `txexpr` library cannot hope to warehouse them all. Why not include it with your citation package?
mbutterick commented 5 years ago (Migrated from github.com)

BTW I follow this rule of thumb. I too have written many of these kinds of list-manipulation functions. I ended up generalizing the repeating patterns in sugar/list, like slicef below:

#lang racket
(require txexpr rackunit sugar/list)

(define/contract (merge-successive-strings elements)
  (txexpr-elements? . -> . txexpr-elements?)
  (apply append
         (for/list ([slice (in-list (slicef elements string?))])
                   (match slice
                     [(cons (? string?) _)
                      (list (apply string-append slice))]
                     [_ slice]))))

(module+ test
  (check-equal? (merge-successive-strings '()) '())
  (check-equal? (merge-successive-strings '("a")) '("a"))
  (check-equal? (merge-successive-strings '(a)) '(a))
  (check-equal? (merge-successive-strings '(a "b")) '(a "b"))
  (check-equal? (merge-successive-strings '("a" "b")) '("ab"))
  (check-equal? (merge-successive-strings '("a" a "b")) '("a" a "b"))
  (check-equal? (merge-successive-strings '("a" "b" " " "c")) '("ab c"))
  (check-equal? (merge-successive-strings '("a" "b" a " " b "c" c d "e" " f" g)) '("ab" a " " b "c" c d "e f" g)))
BTW I follow this rule of thumb. I too have written many of these kinds of list-manipulation functions. I ended up generalizing the repeating patterns in `sugar/list`, like `slicef` below: ```racket #lang racket (require txexpr rackunit sugar/list) (define/contract (merge-successive-strings elements) (txexpr-elements? . -> . txexpr-elements?) (apply append (for/list ([slice (in-list (slicef elements string?))]) (match slice [(cons (? string?) _) (list (apply string-append slice))] [_ slice])))) (module+ test (check-equal? (merge-successive-strings '()) '()) (check-equal? (merge-successive-strings '("a")) '("a")) (check-equal? (merge-successive-strings '(a)) '(a)) (check-equal? (merge-successive-strings '(a "b")) '(a "b")) (check-equal? (merge-successive-strings '("a" "b")) '("ab")) (check-equal? (merge-successive-strings '("a" a "b")) '("a" a "b")) (check-equal? (merge-successive-strings '("a" "b" " " "c")) '("ab c")) (check-equal? (merge-successive-strings '("a" "b" a " " b "c" c d "e" " f" g)) '("ab" a " " b "c" c d "e f" g))) ```
sanchom commented 5 years ago (Migrated from github.com)

Okay, that sounds right. And thanks for the refactor!

Okay, that sounds right. And thanks for the refactor!
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: mbutterick/txexpr#12
Loading…
There is no content yet.