Author Topic: Foreach to mapcar and apply  (Read 2120 times)

0 Members and 1 Guest are viewing this topic.

d2010

  • Bull Frog
  • Posts: 326
Foreach to mapcar and apply
« on: May 17, 2021, 01:04:39 PM »
How I can transform the "foreach to mapcar" or apply on big-list?
You can replace "? ? ?" with  the answer
I need no more loby-variabile used isInside foreach?
 :rolleyes2:
Code: [Select]
(setq $rr "")
 (foreach byte big-list
         (setq;
  loby (rem byte 256)
$rr (strcat $rr (chr loby)))
)

Code - Text: [Select]
  1. (setq $rr "")
  2. (setq $rr (mapcar 'strcat+chr ??? big-list))
  3. [code]
  4. Thank you.
  5.  
  6.  
Big-list contain '(100 451 400 343 444 4566 234 1023 44 1092 12 55 89 34)
« Last Edit: May 17, 2021, 01:09:34 PM by d2010 »

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Foreach to mapcar and apply
« Reply #1 on: May 17, 2021, 02:03:17 PM »
MAPCAR is a looping function like WHILE/FOREACH.

MAPCAR should be combined with a function--in this case--so you can localize variables; we can use a nameless function by using LAMBDA.

Code - Auto/Visual Lisp: [Select]
  1.   '(lambda ( byte / loby )
  2.       (setq loby (rem byte 256)
  3.             $rr (strcat $rr (chr loby))))
  4. big-list)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Foreach to mapcar and apply
« Reply #2 on: May 17, 2021, 02:08:49 PM »
You can also build your own MAPCAR.

Here are two versions for you:

Code - Auto/Visual Lisp: [Select]
  1. (defun mapItter (pros items / cntr nlst lstlength)
  2.   (setq cntr 0 lstlength (length items))
  3.   (while (< cntr lstlength)
  4.     (setq nlst (cons (pros (nth cntr items)) nlst))
  5.      (setq cntr (1+ cntr)))
  6.   (reverse nlst) )
  7.  
  8. (mapItter
  9.   (lambda ( byte / loby )
  10.       (setq loby (rem byte 256)
  11.             $rr (strcat $rr (chr loby))))
  12.   big-list )
  13.  
  14. (defun map (proc items)
  15.  (if (null items)
  16.     nil
  17.     (cons (proc (car items))
  18.           (map proc (cdr items)))) )
  19.  
  20. (map
  21.   (lambda ( byte / loby )
  22.       (setq loby (rem byte 256)
  23.             $rr (strcat $rr (chr loby))))
  24.   big-list )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Foreach to mapcar and apply
« Reply #3 on: May 17, 2021, 02:45:07 PM »
Another method using only APPLY, MAPCARs and a few functions.

Code - Auto/Visual Lisp: [Select]
  1. (defun list:Remove (e l)
  2.   ;; Author: Matt Stachoni
  3.   (apply
  4.     'append
  5.     (subst nil (list e) (mapcar 'list l)))  )
  6.  
  7. (defun join (l)
  8.  (apply
  9.   'strcat
  10.   (mapcar 'chr l)) )
  11.  
  12. (list:Remove 256 big-list)
  13. (join big-list)
  14.  
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Foreach to mapcar and apply
« Reply #4 on: May 18, 2021, 03:51:21 PM »
Posting time results in case anyone was interested.

Code - Auto/Visual Lisp: [Select]
  1. ( (lambda ( / biglist )
  2.     (defun dostuff1 (l / $rr)
  3.       (setq $rr "")
  4.       (mapcar
  5.         '(lambda ( byte / loby )
  6.            (setq loby (rem byte 256)
  7.                  $rr (strcat $rr (chr loby))))
  8.         l)
  9.       )
  10.     (defun dostuff2 (l)
  11.       (apply 'strcat
  12.              (mapcar 'chr
  13.                      (apply 'append
  14.                             (subst nil (list 256) (mapcar 'list l))))) )
  15.     (setq biglist '(84 104 105 115 256 32 105 115 256 32 97  256 32 98 105 103 256 32 108 111 110 103  256 32 116 101 120 116  256 32 115 116 114 105 110 103 32 119 104 105 99 104 32 119 105 108 108 32 114 101 115 117 108 116 32 105 110 32 97 32 115 109 97 108 108 32 108 105 115 116 32 111 102 32 110 117 109 98 101 114 115 46))
  16.     (benchmark
  17.       '(
  18.         (dostuff1 biglist)
  19.         (dostuff2 biglist)
  20.         )
  21.       )
  22.     )
  23.  )

Code: [Select]
------------------------------------------------------
Elapsed milliseconds / relative speed for 8192 iteration(s):

    (DOSTUFF2 BIGLIST).....1188 / 1.37 <fastest>
    (DOSTUFF1 BIGLIST).....1625 / 1.00 <slowest>
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Foreach to mapcar and apply
« Reply #5 on: May 18, 2021, 06:20:06 PM »
You may find vl-list->string quicker, i.e.:
Code - Auto/Visual Lisp: [Select]
  1. (defun dostuff3 ( l )
  2.     (vl-list->string (vl-remove 0 (mapcar '(lambda ( x ) (rem x 256)) l)))
  3. )

Bench:
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):
  2.  
  3.     (DOSTUFF3 BIGLIST)......1170 / 14.13 <fastest>
  4.     (DOSTUFF2 BIGLIST)......8003 / 2.07
  5.     (DOSTUFF1 BIGLIST).....16536 / 1.00 <slowest>
« Last Edit: May 18, 2021, 06:23:27 PM by Lee Mac »

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Foreach to mapcar and apply
« Reply #6 on: May 19, 2021, 04:09:55 PM »
Beat you on a technicality. -i.e. shouldn't that be just:
Code - Auto/Visual Lisp: [Select]
  1. (defun dostuff4 (l)
  2.        (vl-list->string (vl-remove 256 l)) )

Code - Auto/Visual Lisp: [Select]
  1. ( (lambda ( / biglist dostuff1 dostuff2 dostuff3 dostuff4)
  2.     (defun dostuff1 (l / $rr)
  3.       (setq $rr "")
  4.       (mapcar
  5.         '(lambda ( byte / loby )
  6.            (setq loby (rem byte 256)
  7.                  $rr (strcat $rr (chr loby))))
  8.         l)
  9.       )
  10.     (defun dostuff2 (l)
  11.       (apply 'strcat
  12.              (mapcar 'chr
  13.                      (apply 'append
  14.                             (subst nil (list 256) (mapcar 'list l))))) )
  15.      (defun dostuff3 ( l )
  16.          (vl-list->string (vl-remove 0 (mapcar '(lambda ( x ) (rem x 256)) l)))
  17.      )
  18.  
  19.      (defun dostuff4 (l)
  20.        (vl-list->string (vl-remove 256 l)) )
  21.  
  22.     (setq biglist '(84 104 105 115 256 32 105 115 256 32 97  256 32 98 105 103 256 32 108 111 110 103  256 32 116 101 120 116  256 32 115 116 114 105 110 103 32 119 104 105 99 104 32 119 105 108 108 32 114 101 115 117 108 116 32 105 110 32 97 32 115 109 97 108 108 32 108 105 115 116 32 111 102 32 110 117 109 98 101 114 115 46))
  23.     (benchmark
  24.       '(
  25.         (dostuff1 biglist)
  26.         (dostuff2 biglist)
  27.         (dostuff3 biglist)
  28.         (dostuff4 biglist)
  29.         )
  30.       )
  31.     )

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

    (DOSTUFF4 BIGLIST)......1781 / 8.49 <fastest>
    (DOSTUFF3 BIGLIST)......4484 / 3.37
    (DOSTUFF2 BIGLIST).....10703 / 1.41
    (DOSTUFF1 BIGLIST).....15125 / 1.00 <slowest>
 ---- In memory of Michael Puckett. ----

However, the DOSTUFF4 does not meet the OP specifications (but I still win).
 
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Foreach to mapcar and apply
« Reply #7 on: May 19, 2021, 06:43:43 PM »
Beat you on a technicality. -i.e. shouldn't that be just:
Code - Auto/Visual Lisp: [Select]
  1. (defun dostuff4 (l)
  2.        (vl-list->string (vl-remove 256 l)) )

But that won't reduce numbers >255 to within the range 1-255?

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Foreach to mapcar and apply
« Reply #8 on: May 19, 2021, 06:49:30 PM »
I get an error with the 'dostuff4' using the OP's list:
Quote
DOSTUFF1
DOSTUFF2
DOSTUFF3
DOSTUFF4
(100 451 400 343 444 4566 234 1023 44 1092 12 55 89 34)
("d" "d" "dÐ" "dÐW" "dÐW" "dÐW" "dÐW" "dÐW" "dÐW," "dÐW,D" "dÐW,D\014" "dÐW,D\0147" "dÐW,D\0147Y" "dÐW,D\0147Y\"")
"dÐW,D\0147Y\""
"dÐW,D\0147Y\""
; error: bad char code in list: 451

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

d2010

  • Bull Frog
  • Posts: 326
Re: Foreach to mapcar and apply
« Reply #9 on: May 20, 2021, 05:28:14 AM »
Please you do not more  replay, because this is finally-solution.
Thank you..God bless you.

You may find vl-list->string quicker, i.e.:
Code - Auto/Visual Lisp: [Select]
  1. (defun dostuff3 ( l )
  2.     (vl-list->string (vl-remove 0 (mapcar '(lambda ( x ) (rem x 256)) l)))
  3. )
Bench:

« Last Edit: May 22, 2021, 07:57:12 AM by d2010 »

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Foreach to mapcar and apply
« Reply #10 on: May 20, 2021, 10:03:32 AM »
I get an error with the 'dostuff4' using the OP's list:
...

...
But that won't reduce numbers >255 to within the range 1-255?

But I still win right?

Actually the reason I posted the initial time-test was because I thought the results were counter intuitive -i.e. a function that processed the list 2 times more then another was actually faster. So along those lines I have one last trick I could employ to show something interesting. Please note though, this is such a small (minor) trick but it could still have a large impact on certain situations (which, I do not remember -i.e. large/small lists, etc).

Take a look at versions #5 and #6 (and #3) and these numbers can shift with multiple runs (and even the amt of memory your acad has--from heavy use or new instances of-).  I'm tapped out of tricks on this but if you know of/discover anything else interesting please share.
Code - Auto/Visual Lisp: [Select]
  1. ( (lambda ( / biglist dostuff1 dostuff2 dostuff3 dostuff4)
  2.     (defun dostuff1 (l / $rr)
  3.       (setq $rr "")
  4.       (mapcar
  5.         '(lambda ( byte / loby )
  6.            (setq loby (rem byte 256)
  7.                  $rr (strcat $rr (chr loby))))
  8.         l)
  9.       )
  10.     (defun dostuff2 (l)
  11.       (apply 'strcat
  12.              (mapcar 'chr
  13.                      (apply 'append
  14.                             (subst nil (list 256) (mapcar 'list l))))) )
  15.      (defun dostuff3 ( l )
  16.          (vl-list->string (vl-remove 0 (mapcar '(lambda ( x ) (rem x 256)) l)))
  17.      )
  18.  
  19.      (defun dostuff4 (l)
  20.        nil
  21.        ;(vl-list->string (vl-remove 256 l))
  22.        )
  23.      
  24.      (defun dostuff5 (l / 8-bit)
  25.        (defun 8-bit (x)
  26.          (mapcar '(lambda ( x ) (rem x 256)) l) )
  27.        (vl-list->string (vl-remove 0 (8-bit l))) )
  28.  
  29.      (defun dostuff6 (l / 8-bit)
  30.        (defun 8-bit (x) (rem x 256))
  31.        (vl-list->string (vl-remove 0 (mapcar (function 8-bit) l))) )
  32.  
  33.     (setq biglist '(84 104 105 115 256 32 105 115 256 32 97  256 32 98 105 103 256 32 108 111 110 103  256 32 116 101 120 116  256 32 115 116 114 105 110 103 32 119 104 105 99 104 32 119 105 108 108 32 114 101 115 117 108 116 32 105 110 32 97 32 115 109 97 108 108 32 108 105 115 116 32 111 102 32 110 117 109 98 101 114 115 46))
  34.     (benchmark
  35.       '(
  36.         (dostuff1 biglist)
  37.         (dostuff2 biglist)
  38.         (dostuff3 biglist)
  39.         ; (dostuff4 biglist)
  40.         (dostuff5 biglist)
  41.         (dostuff6 biglist)
  42.         )
  43.       )
  44.     )
  45.  )

Code: [Select]
Elapsed milliseconds / relative speed for 16384 iteration(s):

    (DOSTUFF6 BIGLIST).....1156 / 1.03 <fastest>
    (DOSTUFF5 BIGLIST).....1172 / 1.01
    (DOSTUFF3 BIGLIST).....1187 / 1.00 <slowest>
 ---- Benchmark utility: In memory of Michael Puckett ----
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org