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

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1421
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: 12912
  • 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: 1421
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: 12912
  • 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: 12912
  • 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.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: How to check if list a part of list?
« Reply #15 on: October 22, 2019, 05:25:40 PM »
Another a little bit slover…

Code: [Select]
(benchmark '(
(vl-some '(lambda (x) (equal x pt 1e-8)) lst)
(vl-member-if '(lambda (x) (equal x pt 1e-8)) Lst)
(vl-some '(lambda (x) (equal x pt 1e-8)) lst)
(vl-member-if '(lambda (x) (equal x pt 1e-8)) Lst)
))
elapsed milliseconds / relative speed for 1024 iteration(s):

    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1484 / 1.04 <fastest>
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1500 / 1.03
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1531 / 1.01
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1547 / 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 #16 on: October 22, 2019, 08:56:01 PM »
Diminishing returns, how do they work?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to check if list a part of list?
« Reply #17 on: October 22, 2019, 09:01:33 PM »
... like building a house on sand.

:nodding:
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 #18 on: October 23, 2019, 03:00:00 AM »
Diminishing returns, how do they work?
I hope I understood the question well...
with vl-member-if there is the advantage of having a return value with more information
Code: [Select]
((lambda (/ i)
   (setq i   '(0. 0. 0.)
         lst nil
         ) ;_ end of setq
   (repeat 8192 (setq lst (cons (setq i (mapcar (function +) i '(0.25 0.25 0.25))) lst)) => )
   (princ)
   ) ;_ end of lambda
)
Code: [Select]
(setq pt (nth    0 lst)) =>  (2048.0 2048.0 2048.0)
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1171 / 1 <fastest>
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1172 / 1 <slowest>

(setq pt (nth   10 lst)) => (2045.5 2045.5 2045.5)
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1218 / 1 <fastest>
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1219 / 1 <slowest>

(setq pt (nth   50 lst)) => (2035.5 2035.5 2035.5)
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1437 / 1 <fastest>
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1438 / 1 <slowest>

(setq pt (nth  100 lst)) => (2023.0 2023.0 2023.0)
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1797 / 1 <fastest>
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1797 / 1 <slowest>

(setq pt (nth  500 lst)) => (1923.0 1923.0 1923.0)
Elapsed milliseconds / relative speed for 8192 iteration(s):
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1015 / 1.03 <fastest>
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1047 / 1 <slowest>

(setq pt (nth 1000 lst)) => (1798.0 1798.0 1798.0)
Elapsed milliseconds / relative speed for 8192 iteration(s):
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1781 / 1.02 <fastest>
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1813 / 1 <slowest>

(setq pt (nth 2000 lst)) => (1548.0 1548.0 1548.0)
Elapsed milliseconds / relative speed for 4096 iteration(s):
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1610 / 1.03 <fastest>
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1656 / 1 <slowest>

(setq pt (nth 3000 lst)) => (1298.0 1298.0 1298.0)
Elapsed milliseconds / relative speed for 2048 iteration(s):
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1156 / 1.04 <fastest>
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1203 / 1 <slowest>

(setq pt (nth 4000 lst)) => (1048.0 1048.0 1048.0)
Elapsed milliseconds / relative speed for 2048 iteration(s):
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1516 / 1.04 <fastest>
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1578 / 1 <slowest>

(setq pt (nth 5000 lst)) => (798.0 798.0 798.0)
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1859 / 1.04 <fastest>
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1937 / 1 <slowest>

(setq pt (nth 6000 lst)) => (548.0 548.0 548.0)
Elapsed milliseconds / relative speed for 1024 iteration(s):
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1109 / 1.04 <fastest>
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1156 / 1 <slowest>

(setq pt (nth 7000 lst)) => (298.0 298.0 298.0)
Elapsed milliseconds / relative speed for 1024 iteration(s):
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1312 / 1.05 <fastest>
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1375 / 1 <slowest>

(setq pt (last lst)) => (0.25 0.25 0.25)
Elapsed milliseconds / relative speed for 1024 iteration(s):
    (VL-SOME (QUOTE (LAMBDA (X) (EQUAL X...).....1516 / 1.04 <fastest>
    (VL-MEMBER-IF (QUOTE (LAMBDA (X) (EQ...).....1578 / 1 <slowest>