guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regexp-split for Guile


From: Chris K. Jester-Young
Subject: Re: regexp-split for Guile
Date: Sun, 21 Oct 2012 12:08:02 -0400
User-agent: Mutt/1.5.21 (2010-09-15)

On Sat, Oct 20, 2012 at 10:16:49AM -0400, Mark H Weaver wrote:
> Sorry, that last example is wrong of course, but both of these examples
> raise an interesting question about how #:limit and #:trim should
> interact.  To my mind, the top example above is correct.  I think the
> last result should be "baz", not "baz  ".
[...]
> Honestly, this question makes me wonder if the proposed 'regexp-split'
> is too complicated.  If you want to trim whitespace, how about using
> 'string-trim-right' or 'string-trim-both' before splitting?  It seems
> more likely to do what I would expect.

Thanks so much for your feedback, Mark! I appreciate it.

Yeah, I think given the left-to-right nature of regex matching, the
only kind of trimming that makes sense is a right trim. And then once
you do that, people start asking for left trim, and mayhem begins. ;-)

I do want to consider the string pre-trimming approach, as it's more
clear what's going on, and is less "magical" (where "magic" is a plus
in the Perl world, and not so much of a plus in other languages).

Thankfully, the string-trim{,-right,-both} functions you mentioned use
substring behind the scenes, which uses copy-on-write. So that solves
one of my potential concerns, which is that a pre-trim would require
copying most of the string.

                        *       *       *

Granted, if you want trimming-with-complicated-regex-delimiter, and
not just whitespace, then your best bet is to trim the output list.
This is slightly more complicated, because my original code simply
uses drop-while before reversing the output list for return, but since
the caller doesn't receive the reversed list, they either have to
reverse+trim+reverse (yuck), or we have to implement drop-right-while
(like you mentioned previously).

In that regard, here's one implementation of drop-right-while (that I
just wrote on the spot):

    (define (drop-right-while pred lst)
      (let recur ((lst lst))
        (if (null? lst) '()
            (let ((elem (car lst))
                  (next (recur (cdr lst))))
              (if (and (null? next) (pred elem)) '()
                  (cons elem next))))))

One could theoretically write drop-right-while! also (I can think of
two different implementation strategies) but it sounds like it's more
work than it's worth.

So, that's our last hurdle: we "just" have to get drop-right-while
integrated into Guile, then we can separate out the splitting and
trimming processes. And everybody will be happy. :-)

Comments welcome,
Chris.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]