Bill Allombert on Wed, 31 Jul 2013 12:34:35 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

variadic GP functions


Dear developers,

I have created a GIT branch 'bill-vararg' which allow to create
GP functions that take a variable number of arguments.

The syntax is as follow:
f(a[..])=sum(i=1,#a,a[i]);
declare a GP function that take a variable number of arguments and return
their sums:
? f(1,2,3)
%2 = 6;
the variable a evaluates to the vector of the argument.

It is possible to add extra argument at the start:
g(s,a[..])=s*sum(i=1,#a,a[i]);
? g(2,3,4)
%4 = 14

As a side effect of the implementation, it is valid to call
apply(f,...) and select(f,...) on a vector of vector of arguments:

? apply(f,[[2,3],[3,4,5]])
%5 = [5, 12]

We might want to provide non-variadic versio of the built-in variadic functions
(print, printf, Strprintf, etc) to allow to use them to build
variadic functions like

printtime(t,f,a[..])=printf("Time %s: %d\n", vStrprintf(f,a), t)

We could also provide a way to convert variadic functions to non-variadic.
Currently this is possible for GP functions using
vtonv(f)=v->apply(f,[v])[1]

? vtonv(f)([3,4,5])
%9 = 12

Cheers,
Bill