[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] irregex-replace return value
From: |
Michele La Monaca |
Subject: |
Re: [Chicken-users] irregex-replace return value |
Date: |
Sun, 2 Mar 2014 12:51:07 +0100 |
Hi Alex,
> I've used irregex-replace{,/all} and equivalents in other
> languages for a long time, and find the current semantics
> most convenient. I can see in some cases wanting to test
> for a replacement, or in irregex-replace-all the number of
> replacements, but it seems to be by far the rarer case
> (varying with individual programming style).
I concur that the current semantic is what is needed most of the time. But most
is not all and so you're basically leaving the not-so-common cases out in the
cold.
I regard these kind of functions as low-level ones. As such, they
should convey as
much information as they can even if that means their usage might be slightly
inconvenient. Upon them, the library or the end-user can build their own shim
functions to build new more convenient semantics. The other way around is,
of course, not possible.
> Your options right now in these cases are to test for the
> match then apply the subst manually, or write a utility to
> do so.
While writing my own version of irregex-replace can be (hopefully) an enjoyable
6-line coding experience (btw, irregex-apply-match is not documented):
(define (my-own-irregex-replace irx s . o)
(let ((m (irregex-search irx s)))
(and m (string-append
(substring s 0 (irregex-match-start-index m 0))
(apply string-append (reverse (irregex-apply-match m o)))
(substring s (irregex-match-end-index m 0) (string-length s))))))
writing a customized version of irregex-replace/all means writing a real
non-elementary program. Now, I am readying a new egg hence doing that will not
change the overall economy. But what if I just needed a quick script to match
something as simple as:
sed -n s/regex/replacement/gp ?
Maybe a fair trade-off could be to have irregex-replace{,/all} return multiple
values. For example:
;; irregex-replace
(values replaced-string (start-index . end-index)) ; replacement
(values replaced-string '()) ; no replacements
;; irregex-replace/all
(values replaced-string number-of-replacements) ; replacement(s)
(values replaced-string 0) ; no replacements
but I am not sure if that implies a performance penalty.
> If you're interested, there's also SRFI 115 currently under
> discussion for standard Scheme regular expressions.
Oh, thanks for the info. I will have a look at that.
Michele