Author Topic: Setting a variable based on contents on list.  (Read 1273 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 1702
  • class keyThumper<T>:ILazy<T>
Re: Setting a variable based on contents on list.
« Reply #15 on: May 23, 2023, 04:53:28 PM »
Quote from:  @ gilles

@Kerry, the (function min) and (function cadr) are useless because 'min' and 'cadr' are already compiled function. IOW, the 'function' function is only usefull with 'lambda' expressions.

You're correct. The redundant function is wasted, even though it works.  I have some similar methods in my library that I've been using for 20+ years.

That's time I'll never get back   :cry:   :-D

The corrected method in Post Reply #3 would be :

Code - Auto/Visual Lisp: [Select]
  1. // return the smallest Y value from a list of points
  2.  
  3. (defun kdub:miny (pointlist)
  4.   (apply 'min (mapcar 'cadr pointlist))
  5. )
  6.  
  7.  


(setq pointlist '( ( 1 -56.8312 8 ) ( 2 -57.3313 7 ) ( 3 -57.9562 6 ) ( 4 -58.4563 5 )))

(kdub:miny pointlist)

;;==>  -58.4563

« Last Edit: May 23, 2023, 05:01:17 PM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.[/i

bruno_vdh

  • Newt
  • Posts: 89
Re: Setting a variable based on contents on list.
« Reply #16 on: May 24, 2023, 05:28:21 AM »
Hi,

I've just implemented one of the sorting methods above and I appreciate everyone's input!

Based on the following idea:
I wanted to show another way with some generic higher order funtions.
Theorically aggregating (folding) a list is cheaper than sorting it because it iterates the list only once.

And taking up the previous proposals of (gile) on reply #10, It is possible to adapt a generic search function based on the model of the function (VL-sort list comparison-function), without having to sort:
Code - Auto/Visual Lisp: [Select]
  1. (defun wanted (l f / a)
  2.   (setq f (eval f)
  3.         a   (car l)
  4.   )
  5.   (foreach x (cdr l)
  6.     (if (f a x) a (setq a x))
  7.   )
  8. )

Search for the largest and smallest Y
Code: [Select]
_$ (setq lst '((288.252 -56.8312 0.0) (288.252 -57.3313 0.0) (288.252 -57.9562 0.0) (288.252 -58.4563 0.0)))
((288.252 -56.8312 0.0) (288.252 -57.3313 0.0) (288.252 -57.9562 0.0) (288.252 -58.4563 0.0))

_$ (wanted lst (function (lambda (a b) (> (cadr a) (cadr b)))))
(288.252 -56.8312 0.0)
_$ (wanted lst (function (lambda (a b) (< (cadr a) (cadr b)))))
(288.252 -58.4563 0.0)




kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 1702
  • class keyThumper<T>:ILazy<T>
Re: Setting a variable based on contents on list.
« Reply #17 on: May 24, 2023, 05:45:18 PM »
For giggles.

Code - Auto/Visual Lisp: [Select]
  1. Benchmarking-V2 :Boundary=500 [M.P.2005 <revised kdub 2005,2014>] .................
  2. Elapsed milliseconds for 16384 iteration(s)/ relative Timing :
  3.  
  4.     (CAR (VL-SORT POINTLIST (QUOTE (LAMB...).....937 / 1.8159 <slowest>: 0.05718994ms Per iteration
  5.     (WANTED POINTLIST (FUNCTION (LAMBDA ...).....797 / 1.5446
  6.     (FOREACH P POINTLIST (IF (< (CADR PM...).....766 / 1.4845
  7.     (FILTER_RANK_SIZE (QUOTE CADR) (QUOT...).....672 / 1.3023
  8.     (GC:MINBY (QUOTE CADR) POINTLIST)............640 / 1.2403
  9.     (MINBY (QUOTE CADR) POINTLIST)...............516 / 1.0000 <fastest>: 0.03149414ms Per iteration
  10.  


Gilles takes the speed prize, again  . . . a bag of black (virtual) jellybeans  :)

Well done Sir !!
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.[/i

JohnK

  • Administrator
  • Seagull
  • Posts: 10286
Re: Setting a variable based on contents on list.
« Reply #18 on: May 24, 2023, 07:03:04 PM »
Did mine make the list?

However, I reserve the right to edit your post/results if mine registers too high up the list--which, considering the company, is highly likely--(putting code up for a speed test against some of you people is like purposely banging your head against a wall).
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 1702
  • class keyThumper<T>:ILazy<T>
Re: Setting a variable based on contents on list.
« Reply #19 on: May 24, 2023, 07:41:57 PM »
Did mine make the list?

However, I reserve the right to edit your post/results if mine registers too high up the list--which, considering the company, is highly likely--(putting code up for a speed test against some of you people is like purposely banging your head against a wall).

Sorry John, I rushed that a bit too much.
I'll add your return-if shortly.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.[/i

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 1702
  • class keyThumper<T>:ILazy<T>
Re: Setting a variable based on contents on list.
« Reply #20 on: May 24, 2023, 07:50:18 PM »
Ta da da dahhh

Code - Auto/Visual Lisp: [Select]
  1. Benchmarking-V2 :Boundary=500 [M.P.2005 <revised kdub 2005,2014>] .................
  2. Elapsed milliseconds for 16384 iteration(s)/ relative Timing :
  3.  
  4.     (RETURN-IF (QUOTE <) POINTLIST (CAR ...).....1781 / 3.2559 <slowest>: 0.10870361ms Per iteration
  5.     (CAR (VL-SORT POINTLIST (QUOTE (LAMB...)......969 / 1.7715
  6.     (WANTED POINTLIST (FUNCTION (LAMBDA ...)......828 / 1.5137
  7.     (FOREACH P POINTLIST (IF (< (CADR PM...)......781 / 1.4278
  8.     (FILTER_RANK_SIZE (QUOTE CADR) (QUOT...)......704 / 1.2870
  9.     (GC:MINBY (QUOTE CADR) POINTLIST).............687 / 1.2559
  10.     (MINBY (QUOTE CADR) POINTLIST)................547 / 1.0000 <fastest>: 0.03338623ms Per iteration
  11.  


 :wink:
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.[/i

JohnK

  • Administrator
  • Seagull
  • Posts: 10286
Re: Setting a variable based on contents on list.
« Reply #21 on: May 24, 2023, 08:17:03 PM »
See wall. Bang head!

300 times worse!?
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

El Jefe

  • Retired
  • Needs a day job
  • Posts: 7921
  • AKA Daniel
Re: Setting a variable based on contents on list.
« Reply #22 on: May 24, 2023, 08:18:01 PM »
a bag of black (virtual) jellybeans  :)
Retired

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 1702
  • class keyThumper<T>:ILazy<T>
Re: Setting a variable based on contents on list.
« Reply #23 on: May 24, 2023, 08:44:05 PM »
See wall. Bang head!

300 times worse!?

No !
3.2559 times as long to run. Still pretty good @  0.10870361ms Per iteration

So stay chilly
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.[/i

bruno_vdh

  • Newt
  • Posts: 89
Re: Setting a variable based on contents on list.
« Reply #24 on: May 25, 2023, 06:03:36 AM »
Hello,

See wall. Bang head!

300 times worse!?

No!
The approach was the right one because you had not sorted
After a slight rewrite:
Code - Auto/Visual Lisp: [Select]
  1. (defun return-if-V2 (proced lst / foo)
  2.   (defun foo (f l a)
  3.     (cond ((null l) a)
  4.           ((f (cadr a) (cadar l)) (foo f (cdr l) a))
  5.           (T (foo f (cdr l) (car l)))
  6.     )
  7.   )  
  8. (foo (eval proced) (cdr lst) (car lst))
  9. )
  10.  
  11. _$ (return-if-V2 '< aList )
  12. (288.252 -58.4563 0.0)
  13.  

Code: [Select]
Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):

    (RETURN-IF-V2 (QUOTE <) ALIST)..............1515 / 3.35 <fastest>
    (RETURN-IF (QUOTE <) ALIST (CAR ALIST)).....5079 / 1 <slowest>

VovKa

  • Water Moccasin
  • Posts: 1557
  • Ukraine
Re: Setting a variable based on contents on list.
« Reply #25 on: May 25, 2023, 06:54:00 AM »
i would benchmark all functions compiled
and i guess bruno's simple foreach will win