introducing named parameters

Notices and updates
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

introducing named parameters

Post by Dmi »

Hi, guys!

I have rediscovered a nice old way for making some functions more familiar :-)
Now you can write something like this:

Code: Select all

(:send-email
  To "dmi@feautec.pp.ru"
  Through "feautec.pp.ru"
  From "losthost@narod.ru"
  Subject "test subj"
  Body "test body")
With the function ":"

Code: Select all

(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:

Code: Select all

(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
Posts: 31
Joined: Mon Sep 18, 2006 7:58 am

Really nice!

Post by gcanyon »

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.

Locked