vargs function (or "r-expression)
[1/6] from: tim-johnsons::web::com at: 6-May-2007 17:14
Hello Rebolers:
NOTE: 'vargs is a "C" term where variable arguments are used
and "r-expression" is a take on the lisp term: "s-expression".
In lisp, you can have variable argument lists and the function call
will be coded something like:
(func arg1 arg2 arg3)
I have a need for an "r-expression", that is - a class of functions that
will be evaluated by 'do as part of a block and can have 0 or more
arguments.
example:
do [vargs "two" 3]
do [vargs "two" 3 "four"]
do [vargs "two" 3 none "four"]
do[vargs]
I'm starting with this:
vargs: func[arg1[string! unset!] arg2[integer! unset!]
arg3[string! logic! none! unset!] arg4[string! logic! none! unset!]][
case/all[
value? 'arg1[?? arg1]
value? 'arg2[?? arg2]
value? 'arg3[?? arg3]
value? 'arg4[?? arg4]
]
]
This approach can work for me, I think, but I would welcome any comments,
observations or caveats.
thanks
tim
--
Tim Johnson <tim-johnsons-web.com>
Palmer, Alaska, USA
[2/6] from: btiffin:rogers at: 7-May-2007 0:40
TmljZSBvbmUgVGltLgoKSSdtIG5vdCBzdXJlIG9mIHRoZSBiZXR0ZXIgd2F5IG9mIGdldHRpbmcg
dW5zZXQhIGluIGFuIGV4cHJlc3Npb24KYnV0IHlvdSBjYW4gdXNlCgpkbyBbdmFyZ3Mgc2V0L2Fu
eSAnYSBbZG8gcHJpbiBbXV0gMiBmYWxzZSAiZm91ciJdIAoKdG8gc2tpcCBzZXR0aW5nIGFyZzEs
IChvciBhcmcyLi4uKSBidXQgeW91IGRlZmluaXRlbHkgZG9uJ3Qgc2tpcCBhbnkgdHlwaW5nLgoK
Q2hlZXJzLApCcmlhbgoKT24gU3VuZGF5IDA2IE1heSAyMDA3IDEzOjE0LCBUaW0gSm9obnNvbiB3
cm90ZToKPiB2YXJnczogZnVuY1thcmcxW3N0cmluZyEgdW5zZXQhXSBhcmcyW2ludGVnZXIhIHVu
c2V0IV0KPiCgoKCgoKCgoGFyZzNbc3RyaW5nISBsb2dpYyEgbm9uZSEgdW5zZXQhXSBhcmc0W3N0
cmluZyEgbG9naWMhIG5vbmUhCj4gdW5zZXQhXV1bIGNhc2UvYWxsWwo+IKCgoKCgoKCgoKCgoKCg
oKB2YWx1ZT8gJ2FyZzFbPz8gYXJnMV0KPiCgoKCgoKCgoKCgoKCgoKCgdmFsdWU/ICdhcmcyWz8/
IGFyZzJdCj4goKCgoKCgoKCgoKCgoKCgoHZhbHVlPyAnYXJnM1s/PyBhcmczXQo+IKCgoKCgoKCg
oKCgoKCgoKB2YWx1ZT8gJ2FyZzRbPz8gYXJnNF0KPiCgoKCgoKCgoKCgoKCgoKCgXQo+IKCgoKCg
oKCgXQo=
[3/6] from: btiffin::rogers::com at: 7-May-2007 0:45
I hope I'm not clogging, but the last reply I sent got sent back here as
trash. It should read.
Nice one Tim.
I'm not sure of the better way of getting unset! in an expression
but you can use
do [vargs set/any 'a [do prin []] 2 false "four"]
to skip setting arg1, (or arg2...) but you definitely don't skip any typing.
Cheers,
Brian
On Sunday 06 May 2007 13:14, Tim Johnson wrote:
[4/6] from: anton::wilddsl::net::au at: 7-May-2007 16:16
Use an empty paren:
f: func [a [any-type!] b [any-type!]][print [value? 'a value? 'b]]
>> f 1
true false
>> f () 1
false true
I sometimes put it at the end of a script to prevent the
last result spilling into the console.
It's just slightly faster to type than 'halt.
Anton.
[5/6] from: santilli::gabriele::gmail::com at: 7-May-2007 12:52
2007/5/6, Tim Johnson <tim-johnsons-web.com>:
> I have a need for an "r-expression", that is - a class of functions that
> will be evaluated by 'do as part of a block and can have 0 or more
<<quoted lines omitted: 4>>
> do [vargs "two" 3 none "four"]
> do[vargs]
You can also just call it with:
(vargs "two" 3)
(vargs "two" 3 "four")
(vargs)
etc. (More Lispy. ;)
However, I don't think this is a great idea when you have many
arguments, and it'll cause problems if you forget the parens. If you
need variable nuber of arguments, the best way is:
vargs: func [args [block!] /local a b c] [
set [a b c] reduce args
?? a ?? b ?? c
]
>> vargs []
a: none
b: none
c: none
== none
>> vargs [1 2]
a: 1
b: 2
c: none
== none
>> vargs [2 + 2 3]
a: 4
b: 3
c: none
== none
This is the starting point to make your ARGS block a dialect. :)
HTH,
Gabriele.
[6/6] from: tim-johnsons::web::com at: 7-May-2007 8:22
On Monday 07 May 2007 10:52, Gabriele Santilli wrote:
> > example:
> > do [vargs "two" 3]
<<quoted lines omitted: 29>>
> == none
> This is the starting point to make your ARGS block a dialect. :)
Thanks Gabriele. That is the elegant way to do it as stand-alone
code.
The function type I am referring to will be used as part of an automatically
generated code set. I thought about using the "unset" method to cut down
on "clutter" if visual expection was done.
Example:
[eval [prn "total_charge" 0 ]]
^ ^ function
| arbitrary marker. Just means "'do the following block"
[literal "just a string"]
^
| arbitrary marker. Just means use the following string as-is.
[eval [prn ["total_charge" 0] ]] ;; Gabriele's method.
I like your method better for the coding of the function itself.
cheers
tim
--
Tim Johnson <tim-johnsons-web.com>
Palmer, Alaska, USA
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted