Reply to topic  [ 5 posts ] 
Feature request: parameter expansion 
Author Message
Yorick Master

Joined: Wed Jun 01, 2005 11:34 am
Posts: 112
Post Feature request: parameter expansion
I'd like to request a feature addition to Yorick: parameter expansion. I'll give an example of proposed syntax and then explain what I mean.

Code:
obj = save(a, b, c);
result1 = examplepos(@obj);
result2 = examplepos(a, b, c);

result3 = examplekw(%obj);
result4 = examplekw(a=a, b=b, c=c);

foo = save(a, b, c);
bar = save(d, e, f);
result5 = exampleposkw(@foo, %bar);
result6 = exampleposkw(a, b, c, d=d, e=e, f=f);


In the above examples, each pair of function calls are identical except the first in each uses my new proposed syntax. The @ symbol indicates that the members in obj should be expanded into positional arguments. The % symbol indicates that the members in obj should be expanded into keyword arguments. And if a function accepts both, you can provide both.

I have zero attachment to the specific syntax I proposed. I'd just like to see this feature implemented in some way, even if it uses substantially different syntax. I often find myself wanting to build up a list of parameters or keywords to pass to a function. I've come up with work-arounds when that happens, but this seems a much more elegant and powerful solution.

It also seems fairly compatible with what I understand of Yorick's internal implementation. Yorick functions get a bunch of items on a stack and the C function is called with nArgs that tell it how many items it should use on the top of the stack. So if @ or % are encountered, it could just push all the object members to the stack and update nArgs to reflect the new stack prior to calling the function. I imagine it's probably more complicated than that, of course, but I'm hoping it's still feasible without being difficult.

As as bonus, it'd be nice to mix real parameters with expandable parameters, potentially multiple times, and have them expand in place:
Code:
foo = save(c, d);
bar = save(f, g);
baz = save(i, j);
result7 = examplebonus(a, b, @foo, e, @bar, h=h, %baz);
result8 = examplebonus(a, b, c, d, e, f, g, h=h, i=i, j=j);

But I imagine that'd be a lot more of a headache, and I think it'd be marginally more useful.

(I imagine this likely falls under "wishful thinking" but figured it doesn't hurt to ask. :))


Thu Mar 06, 2014 11:56 am
Profile
Yorick Master

Joined: Mon Nov 22, 2004 9:43 am
Posts: 354
Location: Livermore, CA, USA
Post Re: Feature request: parameter expansion
I'm not going to add syntax (e.g.- unary @ and % operators) to yorick any time soon. The parser is a nightmare, and I'm afraid to change it.

You are correct that right now, yorick has no way to invoke a function with an indeterminate number of arguments. The wrap_args function gives you a way to do the reverse -- to define a function which accepts and manipulates an arbitrary number of arguments; the older .. and next_args function does something similar for positional arguments only. The most frequent case I've wanted this is to collect up all remaining ..-arguments and invoke another ..-function with all of them. Ditto for keywords. The keywords case arises more frequently, but there is an easy, though slightly clumsy, prescription in that case -- you simply copy all the keywords of the caller in the invocation.

Something like eval(function, arg_object) would be the most straightforward way to get the functionality you suggest. The eval function could have multiple arguments and keywords to get all your variants. If you're willing to restrict it to, say, at most a dozen positional arguments, you can easily write eval as an interpreted function. But the full featured eval would need to be a compiled function.

I note that if you are writing the function being called yourself, you can simply have it test its first argument, and if it is an object, restore the subsequent arguments from that object. I do this fairly often these days.

While I agree with you that this would be occasionally useful, I think you only need it in fairly obscure situations. Usually, you are attempting to write "meta" functions, which need lots of introspection that yorick doesn't really have. Generally, you should be thinking about other approaches if you find yourself needing this kind of thing. Python is dramatically better at these tasks than yorick. I plan to make yorick into a python module eventually (with yorick functions callable from python code and python functions callable from yorick code); I'm tempted to pass on this feature and just let you use python for the things it's good at.


Mon Mar 10, 2014 8:42 pm
Profile
Yorick Master

Joined: Wed Jun 01, 2005 11:34 am
Posts: 112
Post Re: Feature request: parameter expansion
Thanks for the response. A full-featured eval(function, arg_object) sounds wonderful. :) I do usually find ways around the issue, but thought it wouldn't hurt to request the more general case solution. I totally understand if it never happens though.

And especially with a yorick python module in the works. That sounds fantastic to me! Getting to leverage the two languages' strengths together would be a dream come true.


Wed Mar 12, 2014 11:07 am
Profile
Yorick Master

Joined: Tue Mar 07, 2006 10:31 pm
Posts: 125
Location: Meudon, France
Post Re: Feature request: parameter expansion
Hi Dave,

munro wrote:
But the full featured eval would need to be a compiled function.


But there is still no clearly defined (and cleanly designed) API to call interpreted code from compiled code, or is it?

munro wrote:
I plan to make yorick into a python module eventually (with yorick functions callable from python code and python functions callable from yorick code)


Are you serious about it? Do you have a timescale in mind? Would any current Yorick code and Yorick plug-in work in this pyorick? What about gist (which once was, IIRC, compiled into a python module)?

Kind regards, Thibaut.


Wed Nov 12, 2014 8:07 am
Profile WWW
Yorick Master

Joined: Mon Nov 22, 2004 9:43 am
Posts: 354
Location: Livermore, CA, USA
Post Re: Feature request: parameter expansion
See today's announcement forum.

The pyorick python-yorick package requires the fd_read, fd_write, and fd_close functions, which were added to the github repo in August.


Wed Nov 19, 2014 7:37 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.