Author Topic: Remove item according to X factor  (Read 351 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 774
Remove item according to X factor
« on: September 18, 2019, 05:05:21 PM »
Hello guys.

How can I remove a list of coordinates if X coordinate is already matched ?

'((3934.9 1322.47 -1.0e-08) (3934.3 1322.47 -1.0e-08) (3934.9 1323.67 -1.0e-08) (3934.3 1323.67 -1.0e-08) (3931.03 1323.08 0.0) (3937.75 1323.08 0.0))

Final result must be like this:

'((3934.9 1322.47 -1.0e-08) (3934.3 1322.47 -1.0e-08) (3931.03 1323.08 0.0) (3937.75 1323.08 0.0))

Thank you in advance.

MP

  • Seagull
  • Posts: 17479
Re: Remove item according to X factor
« Reply #1 on: September 18, 2019, 10:53:36 PM »
If I understood correctly here's a recursive one I quickly knocked out that employs a fuzz factor since you're dealing with real (imprecise) numbers:

Code: [Select]
(defun foo ( pts fuzz / i x )
    (if (setq x (caar pts) i (car pts))
        (cons i (foo (vl-remove-if '(lambda (p) (equal x (car p) fuzz)) (cdr pts)) fuzz))
    )
)

Test:

(foo
   '(   (3934.9  1322.47 -1.0e-08)
        (3934.3  1322.47 -1.0e-08)
        (3934.9  1323.67 -1.0e-08)
        (3934.3  1323.67 -1.0e-08)
        (3931.03 1323.08  0.0)
        (3937.75 1323.08  0.0)
    )
    0.01 ;; don't forget the fuzz
)


Result:

(
    (3934.9  1322.47 -1.0e-08)
    (3934.3  1322.47 -1.0e-08)
    (3931.03 1323.08  0.0)
    (3937.75 1323.08  0.0)
)


FWIW ... cheers.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Coder

  • Swamp Rat
  • Posts: 774
Re: Remove item according to X factor
« Reply #2 on: September 19, 2019, 02:31:38 AM »
Beautiful. Thank you MP.

Is there any other way to go with like foreach or even mapcar to get the same result than recursive ? since recursive is very difficult to me to understand at the meantime.

Lee Mac

  • Seagull
  • Posts: 12285
  • London, England
Re: Remove item according to X factor
« Reply #3 on: September 19, 2019, 07:18:09 AM »
Here's an iterative variant -
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l f / r )
  2.     (foreach x l
  3.         (or (vl-some '(lambda ( y ) (equal (car x) (car y) f)) r)
  4.             (setq r (cons x r))
  5.         )
  6.     )
  7.     (reverse r)
  8. )

Coder

  • Swamp Rat
  • Posts: 774
Re: Remove item according to X factor
« Reply #4 on: September 19, 2019, 07:49:40 AM »
Another beautiful codes.
Thank you Lee for this clear example for me to understand.

BIGAL

  • Bull Frog
  • Posts: 353
  • 30 + years of using Autocad
Re: Remove item according to X factor
« Reply #5 on: September 22, 2019, 01:32:38 AM »
Another alternative not as good as lee's is to vl-sort the lst then you just compare nth item pairs.
A man who never made a mistake never made anything