Author Topic: GetX With Default  (Read 9907 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
GetX With Default
« on: October 18, 2010, 01:06:09 PM »
This code is from a little while ago, written as a generic function to get user input with a 'remembered' default.

I would like to share it here and open the floor to suggestions for improvement or perhaps alternatives that you guys use for the same purpose  :-)

Code: [Select]
(defun LM:GetXWithDefault ( _function _prompt _symbol _default / _toString )
  ;; © Lee Mac 2010
 
  (setq _toString
    (lambda ( x )
      (cond
        ( (eq getangle _function) (angtos x) )
        ( (eq 'REAL (type x))       (rtos x) )
        ( (eq 'INT  (type x))       (itoa x) )
        ( x )
      )
    )
  )
 
  (set _symbol
    (
      (lambda ( input ) (if (or (not input) (eq "" input)) (eval _symbol) input))
      (_function (strcat _prompt "<" (_toString (set _symbol (cond ( (eval _symbol) ) ( _default )))) "> : "))
    )
  )
)

Example:
Code: [Select]
(defun c:test nil
  (LM:GetXWithDefault getreal "\nEnter Number " '*num* 1.0)
)

« Last Edit: October 18, 2010, 01:14:08 PM by Lee Mac »

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Re: GetX With Default
« Reply #1 on: October 18, 2010, 01:21:50 PM »
Practical application: Not the greatest idea (I thought about it myself). Off the top of my head; You will have a problem with name spaces i would imagine.

Academically, I agree with the principle.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: GetX With Default
« Reply #2 on: October 18, 2010, 01:32:18 PM »
Thanks for your input Se7en - I considered it for use when multiple prompts are issued in a program, each perhaps using different functions/prompts.

When you speak about problems with namespaces, would this take effect if the application were to be compiled with a separate namespace, or were you talking in general?

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Re: GetX With Default
« Reply #3 on: October 18, 2010, 02:24:52 PM »
Compiled. but i never researched it. You will have to do that yourself.

As always, I am always cautious but this is how i would/did it (this is in my notes on the subject, however i never applied or used it).

Code: [Select]
(defun aif ( var expr iffalse )
    ;; anaphoric-if
    ;;
    ;; ported to AutoLisp from ?comonLisp?
    ;; I dont know the whole extents of the CL`aif' function, im only
    ;; working from one small definition.
    ;;
    ;; EX:
    ;;     (aif 'a (getint "\nEnter new value [1]: ") 1)
    ;;
    ;; OR (if you want to recall `a'...)
    ;; (aif 'a
    ;;      (getint
    ;;        (strcat
    ;;         "\nEnter new value [" (if a (rtos a 2 0) (rtos 1 2 0)) "]: "))
    ;;      (if a a 1) )
    ;;
    (set var (cond (expr) (iffalse))) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Re: GetX With Default
« Reply #4 on: October 18, 2010, 08:28:43 PM »
Sorry I am am still a bit busy but I wanted to tell you to check the way your procedure evaluates. -i.e. Try to initiate a call and cancel it in the mid of the prompt. ...You may end up with a nasty (hard to track down) bug.

Quote
Command: (LM:GetXWithDefault getreal "\nEnter Number " '*num* 1.0)

Enter Number <1.0000> : *Cancel*
; error: Function cancelled

Command: !*num*
1.0

Command: (aif 'a (getint "\nEnter new value [1]: ") 1)

Enter new value [1]: *Cancel*
; error: Function cancelled

Command: !a
nil
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: GetX With Default
« Reply #5 on: October 18, 2010, 09:45:38 PM »

It's really interesting to see your evolution Lee.
Reminds be of the phases I went through 15 or 20 years ago ; and similar growth by several others here and elsewhere over the years.

and those phases ARE a good thing, 'cause they provide a solid conceptual base to work from.

You'll probably change your mind about do-all getx functions in favour of purpose specific bomb-proof methods for each type.

case in point : adding initget and keyword options to each oh the get options.

I recall a couple of conversations between CAB and myself that were particularly productive in that regard.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: GetX With Default
« Reply #6 on: October 18, 2010, 10:37:22 PM »
Ah yes, good times they were. I learned a lot from those conversations.  8-)

http://www.theswamp.org/index.php?topic=6992.0
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetX With Default
« Reply #7 on: October 19, 2010, 12:35:37 AM »
A limit would be any additional arguments (basepoint for getdist, T to allow strings with spaces for getstring, etc.).
Granted, this could easily be avoided by appending a list with the message and eval'ing it.

eg.
Code: [Select]
(eval (append (list getstring T) (list (strcat _prompt "blah blah"))))
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Re: GetX With Default
« Reply #8 on: October 19, 2010, 09:21:18 AM »
@alanjt
I'm not sure i follow what you mean. Are you referring to this structure of function call?
As in: "(LM:GetXWithDefault getreal "\nEnter Number " '*num* 1.0)"

If so, i highly recommend using my method (where the "procedure call" is its own entity in itself).
(aif 'a (getint "\nEnter new value [1]: ") 1)


TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetX With Default
« Reply #9 on: October 19, 2010, 09:34:02 AM »
I'm not disagreeing with you.
I was just pointing out a hole in Lee's routine.

eg.
Code: [Select]
(LM:GetXWithDefault getstring "\nSpecify string " '*string* "something")With the above, one could only type wordswithoutspaces. Any option for hidden arguments is removed.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Re: GetX With Default
« Reply #10 on: October 19, 2010, 09:41:29 AM »
Yes, okay. I understood you then.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetX With Default
« Reply #11 on: October 19, 2010, 09:47:01 AM »
Yes, okay. I understood you then.
I like your method, very interesting approach.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Re: GetX With Default
« Reply #12 on: October 19, 2010, 10:21:27 AM »
Yes, okay. I understood you then.
I like your method, very interesting approach.

*lol* you would be the first (Thank you).

If you like that, then you may like this (?related?).

Code: [Select]
(defun let ( bindings body / replace )
  ;; let
  ;; evaluate process with localized variables.
  ;;
  ;; Ported to AutoLisp By: John (7)
  ;;
  ;; Explination of Let:
  ;; [ http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3.2 ]
  ;;
  ;;
  ;; Synopsis: let <bindings> <body>
  ;;
  ;;  (let '((a (+ 1 (* x y)))
  ;;         (b (- 1 y)))
  ;;     '(+ (* x (square a))
  ;;         (* y b)
  ;;         (* a b)))
  ;;  ~~>
  ;;  ((LAMBDA nil (+ (* X (SQUARE (+ 1 (* X Y))))
  ;;  (* Y (- 1 Y))
  ;;  (* (+ 1 (* X Y)) (- 1 Y)))))
  ;;
  ;;  ==> Evaluated value
  ;;
  ;; EX1:
  ;;  (setq x 5)
  ;;  (+ (let '((x 3))
  ;;          '(+ x (* x 10)))
  ;;   x)
  ;; 
  ;;  ==> 38
  ;; 
  ;; EX2:
  ;;  (setq x 2 y 4)
  ;;  (let '((x 3)
  ;;         (y (+ x 2)))
  ;;       '(* x y))
  ;; 
  ;;  ==> 12
  ;;
  ;; EX3:
  ;;
  ;;  ( (lambda ( / let )
  ;;    (defun let (bindings body)
  ;;      (eval
  ;;        (cons (append (list 'lambda (mapcar 'car bindings)) body)
  ;;              (mapcar 'cadr bindings))) )
  ;;
  ;;    (let
  ;;      '((ss-vp
  ;;          (ssget "x"
  ;;                 (list '(0 . "VIEWPORT")
  ;;                       '(-4 . "/=")
  ;;                       '(68 . 1)
  ;;                       (cons 410 (getvar "ctab"))))))
  ;;      '((if (null ss-vp)
  ;;          (progn
  ;;            (alert "\n No VIEWPORTS in current layout")
  ;;            (princ) )
  ;;          ss-vp))
  ;;      )
  ;;    )
  ;; )
  ;;
  ;; ==>
    (eval
        (cons (append (list 'lambda (mapcar 'car bindings)) body)
              (mapcar 'cadr bindings))) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Re: GetX With Default
« Reply #13 on: October 19, 2010, 10:22:39 AM »
I should mention that these are purely academic and not really practical.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: GetX With Default
« Reply #14 on: October 19, 2010, 12:37:07 PM »
Sorry I am am still a bit busy but I wanted to tell you to check the way your procedure evaluates. -i.e. Try to initiate a call and cancel it in the mid of the prompt. ...You may end up with a nasty (hard to track down) bug.

Quote
Command: (LM:GetXWithDefault getreal "\nEnter Number " '*num* 1.0)

Enter Number <1.0000> : *Cancel*
; error: Function cancelled

Command: !*num*
1.0

Command: (aif 'a (getint "\nEnter new value [1]: ") 1)

Enter new value [1]: *Cancel*
; error: Function cancelled

Command: !a
nil

True, the default symbol is set to the first-time default upon evaluation, but, I don't believe this is an issue as the default symbol will have some value whatever the user presses, (other than Esc). But thanks for pointing it out.

 

Database Error

Please try again. If you come back to this error screen, report the error to an administrator.
Back
Connection Problems

Connection Problems

Sorry, SMF was unable to connect to the database. This may be caused by the server being busy. Please try again later.