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

0 Members and 1 Guest are viewing this topic.

nivuahc

  • Guest
RemoveNth ...
« Reply #15 on: April 23, 2005, 07:49:09 AM »
Quote from: MP
Want for me to 'splain?

BRB (grabbin' a mug o' brew ...)


I do! I do!

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
RemoveNth ...
« Reply #16 on: April 23, 2005, 07:49:59 AM »
Quote
Want for me to 'splain?

Oh heck yea!!
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #17 on: April 23, 2005, 07:57:27 AM »
Let's say we have a list of 3 items '(a b c) and we want to remove the 2nd, or 1th item (b).

Code: [Select]
(RemoveNth 1 '(a b c)) ...

(foo a):
    ;;  i = 0 after assignment, so this
    ;;  returns nil to vl-remove-if, iow,
    ;;  the current item is not removed
    (if (eq 1 (setq i (1+ i)))
        (defun foo (x) nil)
    )
   
(foo b):
    ;;  i = 1 after assignment, so this
    ;;  returns a new definition of foo
    ;;  which is also a non nil value,
    ;;  thus vl-remove-if removes this item
    ;;  from the list
    (if (eq 1 (setq i (1+ i)))
        (defun foo (x) nil)
    )

(foo c):
    ;;  for this and any remaining calls
    ;;  foo returns nil no matter what we
    ;;  throw at it, so vl-remove-if will
    ;;  not remove any more items

Make sense?

Can anyone see where there's room for optimization?

:)
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 #18 on: April 23, 2005, 08:12:07 AM »
Quote
Make sense?

Ok.. now it does. Very cool stuff MP.
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
RemoveNth ...
« Reply #19 on: April 23, 2005, 08:13:58 AM »
Quote
Can anyone see where there's room for optimization?

No I can't, at least not at this time.
TheSwamp.org  (serving the CAD community since 2003)

nivuahc

  • Guest
RemoveNth ...
« Reply #20 on: April 23, 2005, 08:14:06 AM »
Quote from: MP
Can anyone see where there's room for optimization?


I can!

First, you have to take into account the time of day --> early
Then you calculate how much coffee has been ingested --> approximately 20oz.
Do a little math, look at the code, and it's fairly easy to see that

Code: [Select]

(setq drink_more_coffee T)


is desperately needed.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
RemoveNth ...
« Reply #21 on: April 23, 2005, 08:15:00 AM »
Wait.... maybe if you stop the search after you get the first match?
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #22 on: April 23, 2005, 08:16:00 AM »
Quote from: Mark Thomas
Quote
Make sense?

Ok.. now it does.



Quote from: Mark Thomas
Very cool stuff MP.

Thanks man. :oops:
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 #23 on: April 23, 2005, 08:46:58 AM »
Quote from: Mark Thomas
Wait.... maybe if you stop the search after you get the first match?

close or "no cigar" ?
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #24 on: April 23, 2005, 08:51:39 AM »
Close: You can't have vl-remove-if stop iterating the list once it starts, all one can do is try to optimize the predicate function, which is what I've done via (defun foo (x) nil) once the target has been realized.

There's still at least one other thing.

:)
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 #25 on: April 23, 2005, 08:58:01 AM »
If there were no overhead associated with reverse, consider 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)
            (if (< n (1+ (/ len 2)))
                (vl-remove-if 'foo lst)
                (reverse
                    (RemoveNth (- len n 1)
                        (reverse lst)
                    )
                )
            )    
        )  
        (   lst  )
    )
   
)

:P
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 #26 on: April 23, 2005, 09:06:19 AM »
That'll take a picture for me figure out. :D
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #27 on: April 23, 2005, 09:51:29 AM »
Hint -- it never has to count higher than half the length of the list.

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

JohnK

  • Administrator
  • Seagull
  • Posts: 10638
RemoveNth ...
« Reply #28 on: April 23, 2005, 11:44:02 AM »
Hey nice trick!

Optomise huh?! well lets see... I think the first thing i would do is to test a while loop against the vl-remove-if and see which one whips thru faster. (You can stop a while loop while you cant a vl-remove so i would be intrested in seeing those results.) However if the while loop is faster then that would negate your fany defining trick.

If the vl-remove function is faster then i would test to see if a set-lambda is faster then a defun. (Which i kinda doubt, but i think its worth a shot if we can remove a bit of time associated with recaling the function definition.)

But if that fails then i would mabey tackle a tree type of recusrsion. (Operate ont the front and the back of the list at the same time. --BUT that would increase the stack a T O N! so that might not work out either.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
RemoveNth ...
« Reply #29 on: April 23, 2005, 11:48:53 AM »
Go crazy John. :)

Here's my non optimized vanilla lisp version (word wrap will kill it) --

Code: [Select]
(defun RemoveNth ( n lst / len key pair )
    (cond
        (   (zerop n) (cdr lst))
        (   (eq n (1- (setq len (length lst))))
            (reverse (cdr (reverse lst)))
        )
        (   (< (setq key -1.0) n len)
            (mapcar 'cdr
                (append
                    (reverse
                        (cdr
                            (member
                                (setq pair
                                    (nth n
                                        (setq lst
                                            (mapcar
                                               '(lambda (item)
                                                    (cons
                                                        (setq key
                                                            (1+ key)
                                                        )
                                                        item
                                                    )
                                                )
                                                lst            
                                            )
                                        )    
                                    )
                                )
                                (reverse lst)
                            )
                        )
                    )
                    (cdr (member pair lst))
                )
            )    
        )
        (   lst  )
    )
)

Haven't had time to bench any of this stuff, so I don't know what's faster. May prove intellestink!
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst