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

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« on: April 23, 2005, 01:08:08 AM »
Having residual caffiene in my system and thinking about Chuck's discussion earlier today on car | cdr I got to thinking about a real old vanilla lisp version of removenth that I had penned using cdr, member etc. and then dreamed up this little number I thought might make Se7en's forehead twitch a bit.

Code: [Select]
(defun RemoveNth ( n lst / len foo i )

    (defun foo (x)
        (if (eq n (setq i (1+ i)))
            (defun foo (x) nil)
        )  
    )

    (cond
        (   (zerop n) (cdr lst))
        (   (eq n (1- (setq len (length lst))))
            (reverse (cdr (reverse lst)))
        )
        (   (< (setq i -1) n len)
            (vl-remove-if 'foo lst)
        )  
        (   lst  )
    )
   
)

;;  examples of use
 
(setq lst '(0 1 2 3 4 5 6 7))
 
(removenth -1 lst) ;; => (0 1 2 3 4 5 6 7)
 
(removenth 0 lst)  ;; => (1 2 3 4 5 6 7)
 
(removenth 4 lst)  ;; => (0 1 2 3 5 6 7)
 
(removenth 7 lst)  ;; => (0 1 2 3 4 5 6)
 
(removenth 8 lst)  ;; => (0 1 2 3 4 5 6 7)

What do ya think?

:lol:

(Pssst -- there's room for optimization, can ya see it?)

Edit: Updated to reflect this post, much better.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
RemoveNth ...
« Reply #1 on: April 23, 2005, 01:16:13 AM »
heh, I'm pleased It doesn't have comments in it :)

That double defun is tricky
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #2 on: April 23, 2005, 01:22:38 AM »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
RemoveNth ...
« Reply #3 on: April 23, 2005, 01:24:52 AM »
hehehehe ........ LOL

that's great stuff MP.
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #4 on: April 23, 2005, 01:27:37 AM »
Too much coffee today; seriously. :shock:

Thank you Kerry, thank you Mark.

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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
RemoveNth ...
« Reply #5 on: April 23, 2005, 01:28:03 AM »
hehehe .. is that the code monkey ??


is he caffinated too ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #6 on: April 23, 2005, 01:30:35 AM »
He's known as <PeterSellersVoice>'the code minkey'</PeterSellersVoice>.

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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
RemoveNth ...
« Reply #7 on: April 23, 2005, 01:34:22 AM »
That is a sick bit of code Michael. What brand of coffee do you have in the drip bag.?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #8 on: April 23, 2005, 01:35:48 AM »
Instant. Works fast -- no waiting.

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

nivuahc

  • Guest
RemoveNth ...
« Reply #9 on: April 23, 2005, 06:44:52 AM »
<-- sockless

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #10 on: April 23, 2005, 07:17:12 AM »
Do you guys like this variant?

Code: [Select]
(defun RemoveNth ( n lst / len foo i )

    (defun foo (pair)
        (if (eq n (setq i (1+ i)))
            (defun foo (pair) nil)
        )  
    )

    (cond
        (   (zerop n) (cdr lst))
        (   (eq n (1- (setq len (length lst))))
            (reverse (cdr (reverse lst)))
        )
        (   (< (setq i -1) n len)
            (vl-remove-if 'foo lst)
        )  
        (   lst  )
    )
   
)

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

nivuahc

  • Guest
RemoveNth ...
« Reply #11 on: April 23, 2005, 07:22:18 AM »
Now you're just being a show-off :P

:dood:

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #12 on: April 23, 2005, 07:25:47 AM »
Actually, it should read like this --

Code: [Select]
(defun RemoveNth ( n lst / len foo i )

    (defun foo (x)
        (if (eq n (setq i (1+ i)))
            (defun foo (x) nil)
        )  
    )

    (cond
        (   (zerop n) (cdr lst))
        (   (eq n (1- (setq len (length lst))))
            (reverse (cdr (reverse lst)))
        )
        (   (< (setq i -1) n len)
            (vl-remove-if 'foo lst)
        )  
        (   lst  )
    )
   
)

There is still at least one level of optimization that can be done in my mind. Any takers?

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

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
RemoveNth ...
« Reply #13 on: April 23, 2005, 07:35:16 AM »
Quote
Any takers?


yea right ..... I wish.

I'm still hung-up on ...
Code: [Select]
(defun foo (x)
        (if (eq n (setq i (1+ i)))
            (defun foo (x) nil)
        )
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #14 on: April 23, 2005, 07:40:24 AM »
Want for me to 'splain?

BRB (grabbin' a mug o' brew ...)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst