introducing named parameters

Started by Dmi, September 27, 2006, 01:28:56 PM

Previous topic - Next topic

Dmi

Hi, guys!



I have rediscovered a nice old way for making some functions more familiar :-)

Now you can write something like this:
(:send-email
  To "dmi@feautec.pp.ru"
  Through "feautec.pp.ru"
  From "losthost@narod.ru"
  Subject "test subj"
  Body "test body")

With the function ":"
(define (pair lst)
  "(pair lst) - fast pair even number of members"
  (array-list (array (/ (length lst) 2) 2 lst)))

(define-macro (: fun)
  (apply (sym fun MAIN nil)
         (letn (pairs
                 (map (fn (x) (list (eval (sym (first x) fun nil))
                                    (eval (x 1))))
                      (pair (args)))
                m (apply max (map first pairs))
                argl (array (+ 1 m)))
                (dolist (l pairs)
                  (nth-set (l 0) argl (l 1)))
                (array-list argl))))

(global ':)

...and following preparations:
(load "/usr/share/newlisp/smtp.lsp")

(context 'send-email)

(set 'From 0 'To 1 'Subject 2 'Body 3 'Through 4)

(define (send-email:send-email from to subj msg srv)
  (SMTP:send-mail from to subj msg srv))

(context 'MAIN)


In other words, if you want to define a function with named arguments, you must define it as a context default function, and also put in this context all keyword symbols and assign them a positional indexes of corresponding parameters in the function's argument's list.
WBR, Dmi

gcanyon

#1
Out of curiosity (since I barely understand newLISP, let alone macros) I tried extending the example with a synonym:



(set 'From 0 'To 1 'Subject 2 'Body 3 'Through 4 'Using 4)



After that modification, I could make it work with either Through or Using for the server name. So it would be possible to anticipate a wide variety of user choices.