On Sun, Mar 2, 2014 at 8:51 PM, Michele La Monaca <
mikele.chic...@lamonaca.net> wrote:

>
> While writing my own version of irregex-replace can be (hopefully) an
> enjoyable
> 6-line coding experience (btw, irregex-apply-match is not documented):
>

Oops, thanks, I'll document it.

(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.


It's probably not as complicated as you're imagining.
irregex-fold does most of the work:

#;2> (define (my-irregex-replace-all irx str . o)
  (irregex-fold
   irx
   (lambda (i m acc)
     (let ((m-start (irregex-match-start-index m 0)))
       (cons
        (+ 1 (car acc))
        (append (irregex-apply-match m o)
                (if (>= i m-start)
                    (cdr acc)
                    (cons (substring str i m-start) (cdr acc)))))))
   '(0)
   str
   (lambda (i acc)
     (let ((end (string-length str)))
       (values
        (apply
         string-append
         (reverse (if (>= i end)
                      (cdr acc)
                      (cons (substring str i end) (cdr acc)))))
        (car acc))))))

#;3> (my-irregex-replace-all '(+ digit) "one 1 two 22 three 333" "?")

"one ? two ? three ?"

3

; 2 values
I'll consider adding this utility, though.

-- 
Alex
_______________________________________________
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to