Hi Gerard,
I'm using simmilar trick to Christophe's.
example:
f: func [
a [any-type!]
b [any-type!]
c [any-type!]
d [any-type!]
e [any-type!]
][
foreach v bind first :f 'a [
print [
v
either unset? get/any v [
"not defined"
][
get v
]
]
]
]
>> f 1
a 1
b not defined
c not defined
d not defined
e not defined
>> f 1 2 3
a 1
b 2
c 3
d not defined
e not defined
>> f 1 2 3 4 5
a 1
b 2
c 3
d 4
e 5
>>
regards,
Cyphre
----- Original Message -----
From: "Coussement Christophe" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, April 06, 2004 8:12 AM
Subject: [REBOL] Re: [Function] with [Variable number of args]
>
> Hi Gerard,
>
> I once use this little trick for handling a variable number of argument:
>
> f: func [a [integer!] b [unset! integer!]][either value? 'b [a + b][a]]
>
> >> f 1 2
> == 3
> >> f 1
> == 1
>
> Please remark that the type of the argument must be explicitely declared,
otherwise:
>
> f: func [a b][either value? 'b [a + b][a]]
>
> >> f 5 6
> == 11
> >> f 8
> ** Script Error: f is missing its b argument
> ** Near: f 8
>
> Could this be useful to you ?
>
> gretz,
>
> ==christophe
>
> > -----Original Message-----
> > From: Gerard Cote [mailto:[EMAIL PROTECTED]
> > Sent: lundi 5 avril 2004 23:36
> > To: [EMAIL PROTECTED]
> > Subject: [REBOL] [Function] with [Variable number of args]
> >
> >
> >
> > Hi List,
> >
> > while relaxing for a couple of days, I reread one of my old
> > APL books and wanted to try implement some useful mechanisms
> > for vector
> > operations (a vector being a one dimensional array) using
> > simple REBOL scripts. some of these mechanisms are somewhat
> > similar to the
> > higher-level functions Ladislav and other advanced REBOLers
> > have written in the past. I'll give you 2 simple examples below since
> > this is where I found some questions about the way REBOL can
> > or not manage functions with a variable number of arguments.
> >
> > One of the mechanisms I wanted to emulate is simply an
> > extension of the maths operators to take into account two
> > arguments that are
> > not only single values but that also could be a block of
> > values, like in the use of my ++ function :
> >
> > >>++ 1 2
> > == 3
> >
> > >>++ 1 [1 2 3 4 5]
> > ==[2 3 4 5 6]
> >
> > >>++ [6 7 8 9 10] 1
> > ==[7 8 9 10 11]
> >
> > >>++ [1 2 3] [4 5 6]
> > ==[5 7 9]
> >
> > All of this works well for now.
> >
> > The problem is that I also wanted to include in the same
> > function another useful behaviour generally known as +/ in
> > APL, like in
> > this example :
> >
> > >>++ [1 2 3 4] ; 1 + 2 + 3 + 4 == 10
> > ==10
> >
> > But I never was able to say REBOL to use only one argument -
> > leaving the second one optional when necessary - and to not take into
> > account of the second one defined like this :
> > ++: function [arg1 arg2][
> > ...
> >
> > Can someone tell me if there is one simple way to do this
> > with REBOL or if I have to define another independant
> > function to do this.
> > For now what I can do is check if the second arg is an empty
> > block or none but this is not as elegant as it could be.
> >
> > I must admit that I didn't begin to look for an answer by
> > myself yet but I will do this tonight. I found this could be
> > interesting
> > for other newbies too to know more about the subject.
> >
> > Thanks,
> > Gerard
> >
> > --
> > To unsubscribe from this list, just send an email to
> > [EMAIL PROTECTED] with unsubscribe as the subject.
> >
> >
>
> --
> To unsubscribe from this list, just send an email to
> [EMAIL PROTECTED] with unsubscribe as the subject.
>
>
>
--
To unsubscribe from this list, just send an email to
[EMAIL PROTECTED] with unsubscribe as the subject.