Author Topic: lambda with strings  (Read 3254 times)

0 Members and 1 Guest are viewing this topic.

cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
lambda with strings
« on: June 24, 2013, 07:21:38 AM »
Hello!

I need help with a string like this "Cut=\"172.87\"" to get two strings
 -"Cut" 
 -"172.87"
My try is to read string after = 
symbol (x) get "Cut"
symbol (y) get "172.87"

But it doesn´t work in anonym function, I have no expierence with lambda...

Code: [Select]
(setq str "Cut=\"172.87\"")
                                  (function 
                                      (lambda (x y)
                                          (strcat
                                              (if (setq x (cdr (member 61 (reverse (vl-string->list x)))))
                                                  (setq x (vl-list->string (reverse x)))
                                                  )
                                              (if (setq y (cdr (member 61 (vl-string->list y))))
                                                  (vl-list->string (vl-remove-if-not '(lambda(X)(member X (vl-string->list "0123456789."))) y))
                                                  )
                                              )
                                          )
                                      )

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: lambda with strings
« Reply #1 on: June 24, 2013, 07:39:57 AM »
Maybe without lambda ...

Code - Auto/Visual Lisp: [Select]
  1. (setq str "Cut=\"172.87\"")
  2.  
  3. (if (setq p (vl-string-search "=" str))
  4.   (setq cut (substr str 1 p))
  5. )
  6.  
  7. (setq no (vl-list->string
  8.            (vl-remove-if-not
  9.              '(lambda (x)
  10.                 (member x '(46 48 49 50 51 52 53 54 55 56 57))
  11.               )
  12.              (vl-string->list str)
  13.            )
  14.          )
  15. )
  16.  

cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
Re: lambda with strings
« Reply #2 on: June 24, 2013, 07:58:42 AM »
Thanks it works fine and good suggestion, I build in as helper function... :angel:

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: lambda with strings
« Reply #3 on: June 24, 2013, 08:01:14 AM »
Thanks it works fine and good suggestion, I build in as helper function... :angel:

You're welcome , and very happy to help .  :-D

BlackBox

  • King Gator
  • Posts: 3770
Re: lambda with strings
« Reply #4 on: June 24, 2013, 09:37:53 AM »
FWIW - Here's a couple more ways of doing this, and a quick speed test... Each function tested returns a list of strings:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun _foo1 (str / p)
  3.   (if (setq p (vl-string-search "=" str))
  4.     (list
  5.       (substr str 1 p)
  6.       (vl-list->string
  7.         (vl-remove-if-not
  8.           '(lambda (x)
  9.              (member x '(46 48 49 50 51 52 53 54 55 56 57))
  10.            )
  11.           (vl-string->list str)
  12.         )
  13.       )
  14.     )
  15.   )
  16. )
  17.  
  18. (defun _foo2 (str / p)
  19.   (if (setq p (vl-string-search "=" str))
  20.     (list
  21.       (substr str 1 p)
  22.         "\""
  23.         (vl-string-left-trim "\"" (substr str (+ 2 p)))
  24.       )
  25.     )
  26.   )
  27. )
  28.  
  29. (defun _foo3 (str / p)
  30.   (if (setq p (vl-string-search "=" str))
  31.     (list
  32.       (substr str 1 p)
  33.       (rtos (atof (vl-string-translate "\"" "0" (substr str (+ 2 p))))
  34.             2
  35.             2
  36.       )
  37.     )
  38.   )
  39. )
  40.  

... Results from console:
Code - Auto/Visual Lisp: [Select]
  1. _$ (bench '(_foo1 _foo2 _foo3) '("Cut=\"172.87\"") 10000)
  2.  
  3. _FOO1
  4. Elapsed: 577
  5. Average: 0.0577
  6.  
  7. _FOO2
  8. Elapsed: 78
  9. Average: 0.0078
  10.  
  11. _FOO3
  12. Elapsed: 110
  13. Average: 0.0110
  14.  
  15. _$
  16.  
"How we think determines what we do, and what we do determines what we get."

cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
Re: lambda with strings
« Reply #5 on: June 24, 2013, 09:50:53 AM »
cool trials :-D
bench is it your own measure-tool?

BlackBox

  • King Gator
  • Posts: 3770
Re: lambda with strings
« Reply #6 on: June 24, 2013, 09:56:46 AM »
cool trials :-D
bench is it your own measure-tool?

No, it's actually R. Robert Bell's routine (AU Instructor, etc.), but he granted me permission to post it publicly here.
"How we think determines what we do, and what we do determines what we get."

cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
Re: lambda with strings
« Reply #7 on: June 24, 2013, 11:31:39 AM »
Thanx Blackbox: could be useful

BlackBox

  • King Gator
  • Posts: 3770
Re: lambda with strings
« Reply #8 on: June 24, 2013, 11:44:26 AM »
Thanx Blackbox: could be useful

You're welcome; I'm happy to help. :beer:

I do not find any value from this in production directly, but it is a useful utility for testing various ways of doing the same thing... There are many times, when I will not use the fastest, but instead use one close in speed, but easier to read, or provides a slightly different returned value based on usability with/without ObjectDBX, etc.... But one cannot make an informed decision without the applicable information.

Cheers
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: lambda with strings
« Reply #9 on: June 30, 2013, 05:22:12 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun _foo2 (str / p)
  2.   (if (setq p (vl-string-search "=" str))
  3.     (list
  4.       (substr str 1 p)
  5.         "\""
  6.         (vl-string-left-trim "\"" (substr str (+ 2 p)))
  7.       )
  8.     )
  9.   )
  10. )

For what its worth, the left & right string trim could be replaced by a single vl-string-trim:
Code - Auto/Visual Lisp: [Select]
  1. (defun parse ( s / p )
  2.     (if (setq p (vl-string-position 61 s))
  3.         (list (substr s 1 p) (vl-string-trim "\"" (substr s (+ 2 p))))
  4.     )
  5. )