[REBOL] Re: [Function] with [Variable number of args]
From: nitsch-lists:netcologne at: 6-Apr-2004 11:55
On Dienstag, 6. April 2004 11:13, Robert M. M=FCnch wrote:
> On Tue, 6 Apr 2004 08:12:01 +0200, Coussement Christophe
>
> <[Christophe--Coussement--mil--be]> wrote:
> > 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]]
>
> Hi, clever! This is a very cool trick... I hope I remember it. Robert
Drawback is, [ f 1 print now ] is evaluated as [ (f 1 print) now ]. so you
have to write [ (f 1) print now ]. Those unset! are most usefull for console,
where you have implicit brackets. like [ help ] or [ help print ].
Gregg suggested in such a case to put arguments in block, which i prefer.
!>>f: func[args][set[a b] reduce args ? a ? b ]
!>>f [1]
A is an integer of value: 1
B is a none of value: none
!>>f[1 2]
A is an integer of value: 1
B is an integer of value: 2
and can be made smarter for single arg:
!>>f: func[a /local b][if block? a[set [a b] reduce a] ? a ? b]
!>>f 1
A is an integer of value: 1
B is a none of value: none
!>>f [1 2]
A is an integer of value: 1
B is an integer of value: 2
Note the reduce, so you can write
!>>f [1 + 2 3 + 4]
A is an integer of value: 3
B is an integer of value: 7
-Volker