guile-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] add regexp-split


From: Nala Ginrut
Subject: Re: [PATCH] add regexp-split
Date: Fri, 01 Feb 2013 17:24:29 +0800

I found a bug in my previous regexp-split implementation, and fixed now:

-------------------------------------code---------------------------------
(define* (regexp-split regex str #:optional (flags 0))
  (let ((ret (fold-matches 
              regex str (list '() 0 str)
              (lambda (m prev)
                (let* ((ll (car prev))
                       (start (cadr prev))
                       (tail (match:suffix m))
                       (end (match:start m))
                       (s (substring/shared str start end))
                       (groups (map (lambda (n) (match:substring m n))
                                    (iota (1- (match:count m)) 1))))
                  (list `(,@ll ,s ,@groups) (match:end m) tail)))
              flags)))
    `(,@(car ret) ,(caddr ret))))
-------------------------------------end---------------------------------

Now it works fine like Python's regexp-split:
(regexp-split "([^ ]+) (.+)" "a b[^ _]") 
==> ("" "a" "b[^ _]" "")

(regexp-split "([^0-9])([^+/*])" "123+456*/")
==> ("123" "+" "4" "56*/")

I discussed with Andy that regexp-split is a so very common thing that
we should add it into (ice-9 regex).

But considering there're three implementations so far, mine,cky's and
this:
http://lists.gnu.org/archive/html/guile-user/2011-03/msg00007.html

So...I'll left the decision for the maintainers. ;-)

The difference between them maybe: cky's is Perl style (plus Ruby/Java),
and mine is Python's (though I hate Python ;-P).

It's not important to any of them to be chosen, the real meaningful
thing is we do need regexp-split in Guile.

Regards.


Nala Ginrut <nalaginrut <at> gmail.com> writes:

> 
> 
> Now that we have previous thread on this topic, I think it's no need
to format a patch.
> 
> Maybe this will solve the problem:
> 
> (define* (regexp-split regex str #:optional (flags 0))
>   (let ((ret (fold-matches 
> 
>             regex str (list '() 0 str)
> 
>             (lambda (m prev)
> 
>               (let* ((ll (car prev))
> 
>                      (start (cadr prev))
> 
>                      (tail (match:suffix m))
> 
>                      (end (match:start m))
> 
>                      (s (substring/shared str start end))
> 
>                      (groups (map (lambda (n) (match:substring m n))
> 
>                                   (iota (1- (match:count m))))))
> 
>                 (list `(, <at> ll ,s , <at> groups) (match:end m) tail)))
> 
>             flags)))
>     `(, <at> (car ret) ,(caddr ret))))
> 
> On Fri, Dec 30, 2011 at 11:33 PM, Daniel Hartwig <mandyke <at>
gmail.com> wrote:
> On 30 December 2011 21:03, Neil Jerram <neil <at> ossau.homelinux.net>
wrote:
> 
> > Nala Ginrut <nalaginrut <at> gmail.com> writes:
> >
> >> hi guilers!
> >> It seems like there's no "regexp-split" procedure in Guile.
> >> What we have is "string-split" which accepted Char only.
> >> So I wrote one for myself.
> >
> > We've had this topic before, and it only needs a search for
> > "regex-split guile" to find it:
> > http://old.nabble.com/regex-split-for-Guile-td31093245.html.
> >
> Good to see that there is continuing interest in this feature.
> IMO, the implementation here is more elegant and readable for it's use
> of `fold-matches'.  The first implementation from the thread you
> mention effectively rolls it's own version of `fold-matches' over the
> result of `list-matches' (which is implemented using `fold-matches'
> !).
> 
> 
> 
> 
> 
> 




reply via email to

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