Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 23 |
Nodes: | 6 (0 / 6) |
Uptime: | 52:43:45 |
Calls: | 583 |
Files: | 1,139 |
D/L today: |
179 files (27,921K bytes) |
Messages: | 111,617 |
Peter Seibel wrote:
Kenny Tilton <ktilton@nyc.rr.com> writes:
Cool. Now here is a version using loop:
(defun remove-text (text-to-remove text)
(loop with remove-length = (length text-to-remove)
for i = (search text-to-remove text)
then (search text-to-remove text :start2 i)
while i
do (setq text (concatenate 'string
(subseq text 0 i)
(subseq text (+ i remove-length))))
finally (return text)))
Just to point out a useful LOOP idiom, here's another way:
(defun remove-text (text-to-remove text)
(with-output-to-string (s)
(loop
with remove-length = (length text-to-remove)
for prev-end = 0 then (+ start remove-length)
for start = (search text-to-remove text :start2 prev-end)
do (write-string text s :start prev-end :end start)
while start)))
B. Pym wrote:
Peter Seibel wrote:
Kenny Tilton <ktilton@nyc.rr.com> writes:
Cool. Now here is a version using loop:
(defun remove-text (text-to-remove text)
(loop with remove-length = (length text-to-remove)
for i = (search text-to-remove text)
then (search text-to-remove text :start2 i)
while i
do (setq text (concatenate 'string
(subseq text 0 i)
(subseq text (+ i remove-length))))
finally (return text)))
Just to point out a useful LOOP idiom, here's another way:
(defun remove-text (text-to-remove text)
(with-output-to-string (s)
(loop
with remove-length = (length text-to-remove)
for prev-end = 0 then (+ start remove-length)
for start = (search text-to-remove text :start2 prev-end)
do (write-string text s :start prev-end :end start)
while start)))
It can be made shorter if we use a Lispy language instead of CL.
Gauche Scheme
(use srfi-13) ;; string-contains
(use gauche.sequence) ;; size-of (instead of string-length)
(define (remove-text trash text)
(with-output-to-string (^()
(while text
(let1 found (string-contains text trash)
(display (string-copy text 0 found))
(set! text
(and found (subseq text (+ found (size-of trash))))))))))
B. Pym wrote:
B. Pym wrote:
Peter Seibel wrote:
Kenny Tilton <ktilton@nyc.rr.com> writes:
Cool. Now here is a version using loop:
(defun remove-text (text-to-remove text)
(loop with remove-length = (length text-to-remove)
for i = (search text-to-remove text)
then (search text-to-remove text :start2 i)
while i
do (setq text (concatenate 'string
(subseq text 0 i)
(subseq text (+ i remove-length))))
finally (return text)))
Just to point out a useful LOOP idiom, here's another way:
(defun remove-text (text-to-remove text)
(with-output-to-string (s)
(loop
with remove-length = (length text-to-remove)
for prev-end = 0 then (+ start remove-length)
for start = (search text-to-remove text :start2 prev-end)
do (write-string text s :start prev-end :end start)
while start)))
It can be made shorter if we use a Lispy language instead of CL.
Gauche Scheme
(use srfi-13) ;; string-contains
(use gauche.sequence) ;; size-of (instead of string-length)
(define (remove-text trash text)
(with-output-to-string (^()
(while text
(let1 found (string-contains text trash)
(display (string-copy text 0 found))
(set! text
(and found (subseq text (+ found (size-of trash))))))))))
Shorter yet using a slightly modified do*.
;; The "<>" means repeat the preceding expression.
(define (remove-text trash text)
(with-output-to-string (^()
(do* ((start 0 (+ found (size-of trash)))
(found (string-contains text trash start) <>))
((begin (display (string-copy text start found))
(not found)))))))
Given: