Author Topic: RemoveNth ...  (Read 14195 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #30 on: April 25, 2005, 10:04:00 PM »
If anyone is mildy interested I did some tests today and found both versions of the removenth function that use vl-remove-if were approx. 2x faster than the vanilla version, using lists with 1,000 to 1,000,000 items (using a representative spread of 'n' positions).

Having said that, that isn't terrible performance for vanilla lisp.

Challenge extended -- writer a faster variant in either vanilla or visual lisp. Hint - recursive will look prettier but ...

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
RemoveNth ...
« Reply #31 on: April 26, 2005, 12:13:08 AM »
That is interesting, thanks.
Sorry I've been too busy to participate but here are some more variations for your speed test.
Not sure if you had run across them before.

http://www.theswamp.org/index.php?topic=635.0
http://www.theswamp.org/index.php?topic=754.0

<edit: repair broken links>
« Last Edit: December 23, 2008, 06:42:01 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
RemoveNth ...
« Reply #32 on: April 26, 2005, 07:40:20 AM »
Let's not forget about this one

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #33 on: April 26, 2005, 08:12:32 AM »
Holy chyt, wish I'd been here for all those threads; good stuff!

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

daron

  • Guest
RemoveNth ...
« Reply #34 on: April 26, 2005, 08:15:05 AM »
You should've followed Cadaver over here sooner. You would've been here for them.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #35 on: April 26, 2005, 08:19:48 AM »
Came over the first time I saw him reference the place. Of course, I've never been called 'Captain Observant'.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #36 on: April 26, 2005, 08:24:10 AM »
PS: If I'm not mistaken this one will remove every instance of an item from a list; not the same as removing the nth item.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

daron

  • Guest
RemoveNth ...
« Reply #37 on: April 26, 2005, 08:36:05 AM »
True. That was the point of that challenge. I just like how simply effective it was.

taner

  • Guest
Re: RemoveNth ...
« Reply #38 on: December 22, 2008, 09:36:38 PM »
Code: [Select]
(defun removenth (n lst / j)       
  (setq j -1)
  (vl-remove-if '(lambda (x)
   (= n (setq j (1+ j)))
) lst
  )
)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: RemoveNth ...
« Reply #39 on: December 23, 2008, 01:47:16 AM »
Code: [Select]
(defun remove-i (ind lst)
  (if (or (zerop ind) (null lst))
    (cdr lst)
    (cons (car lst) (remove-i (1- ind) (cdr lst)))
  )
)
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: RemoveNth ...
« Reply #40 on: December 23, 2008, 01:57:33 AM »
not recursive
Code: [Select]
(defun remove-nth (l n / a)
 ;;(remove-nth l 20)
 (while (and l (> n 0))
  (setq a (cons (car l) a)
        l (cdr l)
        n (1- n)
  ) ;_  setq
 ) ;_  while
 (append (reverse a) (cdr l))
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: RemoveNth ...
« Reply #41 on: December 23, 2008, 06:32:53 AM »
Code: [Select]
  ;;  CAB 11.16.07 rev. 12.23.08
  ;;  Remove based on pointer list
  (defun RemoveNlst (nlst lst)
    (setq i -1)
    (vl-remove-if '(lambda (x) (vl-position (setq i (1+ i)) nlst)) lst)
  )
 
 
Code: [Select]
  (setq result (RemoveNlst '(1 4) '(0 "A" 2 3 "B" 5 6 7 8 9)))
  ;;  removes A & B
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: RemoveNth ...
« Reply #42 on: December 23, 2008, 06:43:01 AM »
I see the links in my previous post were broken.
Here are the new links:

http://www.theswamp.org/index.php?topic=635.0
http://www.theswamp.org/index.php?topic=754.0
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

zoltan

  • Guest
Re: RemoveNth ...
« Reply #43 on: December 30, 2008, 04:05:21 PM »
Here is something similar I wrote a long time ago to be able to set or remove an atom from in a list.  It's a lot more "brute force" and the speed of it will depend on the position of the element that we are trying to remove. also, there is a lot of overhead from the double reverse of the Add function.

Code: [Select]
(Defun Add ( ATM LST )
 (Reverse (Cons ATM (Reverse LST)) )
)

(Defun SetNth ( IDX LST ITM / cnt return )
 (SetQ cnt 0 )
 (While (< cnt IDX)
  (SetQ return (Add (Car LST) return)
        LST    (Cdr LST)
        cnt    (1+ cnt)
  )
 )
 (SetQ return (Append (Add ITM return) (Cdr LST)) )


 return
)

(Defun RemoveNth ( IDX LST / cnt return )
 (SetQ cnt 0 )
 (While (< cnt IDX)
  (SetQ return (Add (Car LST) return)
        LST    (Cdr LST)
        cnt    (1+ cnt)
  )
 )
 (SetQ return (Append return (Cdr LST)) )

 return
)

It's very similar to ElpanovEvgeniy's solution, but he managed to get around the double reverse!
« Last Edit: December 30, 2008, 04:08:48 PM by zoltan »

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: RemoveNth ...
« Reply #44 on: February 24, 2009, 12:24:22 PM »
Here is my code.
Code: [Select]
(defun remove-n (n lst / a b)
  (setq a lst)
  (repeat n
    (setq b (cons (car a) b)
          a (cdr a)
    )
  )
  (setq a (cdr a))
  (foreach i b
    (setq a (cons i a))
  )
  a
)
By the way, I posted a function  that insert an element in nth position of  a list.

Code: [Select]
(defun insert-n (n element lst / a b)
  (setq a lst)
  (repeat n
    (setq b (cons (car a) b)
          a (cdr a)
    )
  )
  (setq a (cons element a))
  (foreach i b
    (setq a (cons i a))
  )
  a
)
It's very similar as the function "remove-nth".
« Last Edit: February 25, 2009, 01:53:33 AM by highflybird »
I am a bilingualist,Chinese and Chinglish.