From: Vladimir V. Zolotych
Subject: DEL-BLK
Date: 
Message-ID: <3AF1093D.84327426@eurocom.od.ua>
Hello

Some time ago I've been needed something like

$foo =~ S/AB(.+?)CD//g; (1)

where AB and CD can be arbitrary strings.
To get this I've write the following.

(defun del-blk (s a b)
  (let ((n (length s)))
    (flet ((match (p s i)
             (and (>= i (length p))
                  (string= s p :start1 (- i (length p)) :end1 i))))
      (let ((x (loop
                with pos
                for i fixnum from 0 to n
                if (match a s i) do (setq pos (- i (length a)))
                else if (and (match b s i) pos)
                collecting (cons pos i) and do (setf pos nil))))
        (if (null x) s
            (reduce #'(lambda (x y) (concatenate 'string x y))
                    (loop
                     with pos = 0 for (b . e) in (append
                                                  x (list (cons n 0)))
                     collecting (subseq s pos b) do (setf pos e))))))))

I call it the following way

* (del-blk "12<<ab>>34" "<<" ">>")
"1234"
* 

Happened that I mentioned DEL-BLK in argue with Perl people.
They argue "we shouldn't do things being already implemented".
I've try to say "you don't know how they work" but seems w/o
much success. Also they add usually that development time is
valuable for them and they can't spend time to implementing 
such things "manually".

So two my questions to you. How to implement DEL-BLK in
better way and 2) is it possible to say something more significant
opposite such arguments ?

-- 
Vladimir Zolotych                         ······@eurocom.od.ua

From: Raymond Wiker
Subject: Re: DEL-BLK
Date: 
Message-ID: <86elu6luhl.fsf@raw.grenland.fast.no>
"Vladimir V. Zolotych" <······@eurocom.od.ua> writes:

> Hello
> 
> I call it the following way
> 
> * (del-blk "12<<ab>>34" "<<" ">>")
> "1234"
> * 
> 
> Happened that I mentioned DEL-BLK in argue with Perl people.
> They argue "we shouldn't do things being already implemented".
> I've try to say "you don't know how they work" but seems w/o
> much success. Also they add usually that development time is
> valuable for them and they can't spend time to implementing 
> such things "manually".
> 
> So two my questions to you. How to implement DEL-BLK in
> better way and 2) is it possible to say something more significant
> opposite such arguments ?

        I don't know if this is better, but: 

        - it should be useable on any type of sequence

        - it uses built-in (standard) functions

        - it's shorter :-)

(defun del-blk (seq start-marker end-marker)
  (let ((start (search start-marker seq)))
    (when start
      (let ((end (search end-marker seq 
			 :start2 (+ start (length start-marker)))))
	(when end
	  (return-from del-blk 
	    (concatenate (type-of seq)
			 (subseq seq 0 start)
			 (subseq seq (+ end 
                                        (length end-marker)))))))))
  seq) ; default return value: no match

        Note: I'm slightly unhappy of the combination of nested
when's, return-from and a default return value. Suggestions for a more
elegant approach, anyone?

-- 
Raymond Wiker
·············@fast.no
From: Vladimir V. Zolotych
Subject: Re: DEL-BLK
Date: 
Message-ID: <3AF11E79.C5CEA65D@eurocom.od.ua>
Raymond Wiker wrote:

> 
> (defun del-blk (seq start-marker end-marker)
>   (let ((start (search start-marker seq)))
>     (when start
>       (let ((end (search end-marker seq
>                          :start2 (+ start (length start-marker)))))
>         (when end
>           (return-from del-blk
>             (concatenate (type-of seq)
>                          (subseq seq 0 start)
>                          (subseq seq (+ end
>                                         (length end-marker)))))))))
>   seq) ; default return value: no match

Your version produces

* (del-blk "12<<ab<<cd>>34" "<<" ">>")
"1234"
* (del-blk "12<<ab>>34<<cd>>56" "<<" ">>")
"1234<<cd>>56"

While should be

* (del-blk "12<<ab<<cd>>34" "<<" ">>")
"12<<ab34"
* (del-blk "12<<ab>>34<<cd>>56" "<<" ">>")
"123456"

Please note question mark and 'g' in s/AB(.+?)CD//g;

-- 
Vladimir Zolotych                         ······@eurocom.od.ua
From: Marco Antoniotti
Subject: Re: DEL-BLK
Date: 
Message-ID: <y6celu6mpfq.fsf@octagon.mrl.nyu.edu>
Little hack.

==============================================================================
(in-package "CL.PERLISH")

(defun del-blk (seq &key (start-marker "{") (end-marker "}"))
  (multiple-value-bind (processed-seq processedp)
      (del-first-blk seq :start-marker start-marker :end-marker end-marker)
    (if processedp
	(del-blk (del-first-blk processed-seq
				:start-marker start-marker
				:end-marker end-marker)
		  :start-marker start-marker :end-marker end-marker)
	processed-seq)))


(defun del-first-blk (seq &key (start-marker "{") (end-marker "}"))
  (let ((start (search start-marker seq)))
    (if start
	(let ((end (search end-marker seq 
			   :start2 (+ start (length start-marker))))
	      (error-check (search start-marker seq 
				   :start2 (+ start (length start-marker))))
	      )
	  (when (and error-check (< error-check end))
	    (error "Start marker ~S found again before end marker ····@
                    Error sequence chunk ~S."
		   start-marker
		   end-marker
		   (subseq seq start (+ error-check (length start-marker)))))
	  (if end
	      (values (concatenate (type-of seq)
				   (subseq seq 0 start)
				   (subseq seq (+ end (length end-marker))))
		      t)
	      (values seq nil)))
	(values seq nil))))
==============================================================================

* (del-blk "12{abcd}34")
"1234"
* (del-blk "12{ab}34{cd}56")
"123456"
* (del-blk "12{ab{cd}34")
Invoking debugger...

Cheers

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group	tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA			http://bioinformatics.cat.nyu.edu
	       "Hello New York! We'll do what we can!"
			Bill Murray in `Ghostbusters'.
From: Christopher J. Vogt
Subject: Re: DEL-BLK
Date: 
Message-ID: <3AF15F0D.BC1A5199@computer.org>
Raymond Wiker wrote:
> 
> "Vladimir V. Zolotych" <······@eurocom.od.ua> writes:
> 
> > Hello
> >
> > I call it the following way
> >
> > * (del-blk "12<<ab>>34" "<<" ">>")
> > "1234"
> > *
> >
> > Happened that I mentioned DEL-BLK in argue with Perl people.
> > They argue "we shouldn't do things being already implemented".
> > I've try to say "you don't know how they work" but seems w/o
> > much success. Also they add usually that development time is
> > valuable for them and they can't spend time to implementing
> > such things "manually".
> >
> > So two my questions to you. How to implement DEL-BLK in
> > better way and 2) is it possible to say something more significant
> > opposite such arguments ?
> 
>         I don't know if this is better, but:
> 
>         - it should be useable on any type of sequence
> 
>         - it uses built-in (standard) functions
> 
>         - it's shorter :-)
> 
> (defun del-blk (seq start-marker end-marker)
>   (let ((start (search start-marker seq)))
>     (when start
>       (let ((end (search end-marker seq
>                          :start2 (+ start (length start-marker)))))
>         (when end
>           (return-from del-blk
>             (concatenate (type-of seq)
>                          (subseq seq 0 start)
>                          (subseq seq (+ end
>                                         (length end-marker)))))))))
>   seq) ; default return value: no match
> 
>         Note: I'm slightly unhappy of the combination of nested
> when's, return-from and a default return value. Suggestions for a more
> elegant approach, anyone?

Here is an altermative you may like.  Also note that I wouldn't use when w/ return-from, but rather I'd use if.

(defun del-blk (seq start-marker end-marker)
  (let* ((start (search start-marker seq))
	 (end (and start (search end-marker seq
				 :start2 (+ start (length start-marker))))))
    (if end
	(concatenate (type-of seq)
		     (subseq seq 0 start)
		     (subseq seq (+ end (length end-marker))))
      seq)))				; default return value: no match
From: Janis Dzerins
Subject: Re: DEL-BLK
Date: 
Message-ID: <871yq67c7o.fsf@asaka.latnet.lv>
Raymond Wiker <·············@fast.no> writes:

> (defun del-blk (seq start-marker end-marker)
>   (let ((start (search start-marker seq)))
>     (when start
>       (let ((end (search end-marker seq 
> 			 :start2 (+ start (length start-marker)))))
> 	(when end
> 	  (return-from del-blk 
> 	    (concatenate (type-of seq)
> 			 (subseq seq 0 start)
> 			 (subseq seq (+ end 
>                                         (length end-marker)))))))))
>   seq) ; default return value: no match

If it was known that seq is a string, would it be worthwile to use
displaced array instead of subseq?

-- 
Janis Dzerins

  If million people say a stupid thing it's still a stupid thing.