Author Topic: How to check if list a part of list?  (Read 6649 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1420
How to check if list a part of list?
« on: October 20, 2019, 11:10:07 AM »
Hi all

How to check if list pt a part of list lst

Code: [Select]
(setq lst ((505.434 294.582 0.0) (630.636 386.387 0.0) (755.837 478.191 0.0)))
(setq pt (505.434 294.582 0.0))

kpblc

  • Bull Frog
  • Posts: 396
Re: How to check if list a part of list?
« Reply #1 on: October 20, 2019, 11:36:54 AM »
(member pt lst)
?
Sorry for my English.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: How to check if list a part of list?
« Reply #2 on: October 20, 2019, 11:44:54 AM »
If you need some tolerance -

Code - Auto/Visual Lisp: [Select]
  1. (vl-some '(lambda ( x ) (equal x pt 1e-8)) lst)

well20152016

  • Newt
  • Posts: 130
Re: How to check if list a part of list?
« Reply #3 on: October 20, 2019, 06:55:10 PM »
Code - Auto/Visual Lisp: [Select]
  1. (vl-remove-if-not '(lambda (x) (equal pt x 0)) lst)

HasanCAD

  • Swamp Rat
  • Posts: 1420
Re: How to check if list a part of list?
« Reply #4 on: October 21, 2019, 04:50:47 AM »
Working perfect
Thanks All

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to check if list a part of list?
« Reply #5 on: October 21, 2019, 08:58:57 AM »
Using vl-remove-if-not will realize a HUGE unnecessary performance penalty.

List length 1024:

Elapsed milliseconds / relative speed for 32768 iteration(s):

    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...)......1188 / 9.26 <fastest>
    (VL-REMOVE-IF-NOT (QUOTE (LAMBDA (X)...).....11000 / 1.00 <slowest>

List length 8192:

Elapsed milliseconds / relative speed for 8192 iteration(s):

    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).......375 / 56.00 <fastest>
    (VL-REMOVE-IF-NOT (QUOTE (LAMBDA (X)...).....21000 / 1.00 <slowest>
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

kpblc

  • Bull Frog
  • Posts: 396
Re: How to check if list a part of list?
« Reply #6 on: October 21, 2019, 10:15:37 AM »
Code - Auto/Visual Lisp: [Select]
  1. ((lambda (/ i)
  2.    (setq i   '(0. 0. 0.)
  3.          lst nil
  4.          ) ;_ end of setq
  5.    (repeat 8192 (setq lst (cons (setq i (mapcar (function +) i '(0.25 0.25 0.25))) lst)))
  6.    (princ)
  7.    ) ;_ end of lambda
  8.  )
  9. (setq pt '(30. 30. 30.))
  10. (benchmark '((member pt lst)
  11.              (vl-some '(lambda (x) (equal x pt 1e-8)) lst)
  12.              (vl-remove-if-not '(lambda (x) (equal pt x 0)) lst)
  13.              )
  14.            ) ;_ end of benchmark
  15.  
  16. Benchmarking ..............Elapsed milliseconds / relative speed for 2048 iteration(s):
  17.  
  18.     (MEMBER PT LST)..............................1388 / 4.26 <fastest>
  19.     (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....5179 / 1.14
  20.     (VL-REMOVE-IF-NOT (QUOTE (LAMBDA (X)...).....5913 / 1 <slowest>
Sorry for my English.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: How to check if list a part of list?
« Reply #7 on: October 21, 2019, 10:39:08 AM »
... Apples and pears.. , but how about vl-position?

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: How to check if list a part of list?
« Reply #8 on: October 21, 2019, 01:20:28 PM »
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking ..............Elapsed milliseconds / relative speed for 2048 iteration(s):
  2.  
  3.     (MEMBER PT LST)..............................1388 / 4.26 <fastest>
  4.     (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....5179 / 1.14
  5.     (VL-REMOVE-IF-NOT (QUOTE (LAMBDA (X)...).....5913 / 1 <slowest>

If I didn't require a tolerance on the point comparison I'd use member/vl-position too, but doubles are rarely exactly equal  :-)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to check if list a part of list?
« Reply #9 on: October 22, 2019, 11:14:58 AM »
Surprising result, illuminates the impact data diversity can have upon performance. That said, using a remove type function to determine membership triggers my OCD. O.o
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: How to check if list a part of list?
« Reply #10 on: October 22, 2019, 03:54:48 PM »
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking ..............Elapsed milliseconds / relative speed for 2048 iteration(s):
  2.     (MEMBER PT LST)..............................1388 / 4.26 <fastest>
  3.     (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....5179 / 1.14
  4.     (VL-REMOVE-IF-NOT (QUOTE (LAMBDA (X)...).....5913 / 1 <slowest>
Code: [Select]
Elapsed milliseconds / relative speed for 8192 iteration(s):
    (VL-POSITION PT LST)..........................1485 / 8.39 <fastest>
    (MEMBER PT LST)...............................4594 / 2.71
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....11875 / 1.05
    (VL-REMOVE-IF-NOT (QUOTE (LAMBDA (X)...).....12453 / 1 <slowest>

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to check if list a part of list?
« Reply #11 on: October 22, 2019, 04:43:06 PM »
In real world use I don’t believe vl-position or member are practical for floats or lists of floats.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: How to check if list a part of list?
« Reply #12 on: October 22, 2019, 04:47:02 PM »
In real world use I don’t believe vl-position or member are practical for floats or lists of floats.
:yes: :-)

kpblc

  • Bull Frog
  • Posts: 396
Re: How to check if list a part of list?
« Reply #13 on: October 22, 2019, 04:48:03 PM »
The question was "How to check if list pt a part of list lst". We answered this question :)
Sorry for my English.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: How to check if list a part of list?
« Reply #14 on: October 22, 2019, 05:05:50 PM »
In real world use I don’t believe vl-position or member are practical for floats or lists of floats.

Completely agree. Whenever working with AutoLISP reals, fuzzy comparisons are a must IMO; using member/vl-position to perform tests against what are essentially numerical approximations is like building a house on sand.