问题
In the Io programming language, is there an equivalent to lisp's apply function.
So for example I have a method to wrap writeln :
mymeth := method(
//do some extra stuff
writeln(call message arguments))
)
At the moment this just prints the list, and doesn't evaluate it's contents as if they were it's own args.
回答1:
Thanks to that person who suggested evalArgs (not sure where your comment went).
Anyway that has resolved for my situation, although unfortunately not in general I guess.
You can achieve what I describe by doing :
writeln(call evalArgs join)
This evaluates all arguments, and then joins the results into a single string.
回答2:
is there an equivalent to lisp's apply function?
Have a look at perform & performWithArgList methods.
To step back a bit you can replicate Lisp's FUNCALL
in a few different ways in Io:
1 - Passing a function ie. block():
plotf := method (fn, min, max, step,
for (i, min, max, step,
fn call(i) roundDown repeat(write("*"))
writeln
)
)
plotf( block(n, n exp), 0, 4, 1/2 )
2 - Passing a message:
plotm := method (msg, min, max, step,
for (i, min, max, step,
i doMessage(msg) roundDown repeat(write("*"))
writeln
)
)
plotm( message(exp), 0, 4, 1/2 )
3 - passing the function name as a string:
plots := method (str, min, max, step,
for (i, min, max, step,
i perform(str) roundDown repeat(write("*"))
writeln
)
)
plots( "exp", 0, 4, 1/2 )
So from all this you could create a Lisp APPLY
like so:
apply := method (
args := call message argsEvaluatedIn(call sender)
fn := args removeFirst
performWithArgList( fn, args )
)
apply( "plotf", block(n, n exp), 0, 4, 1/2 )
apply( "plotm", message(exp), 0, 4, 1/2 )
apply( "plots", "exp", 0, 4, 1/2 )
来源:https://stackoverflow.com/questions/4419779/io-language-apply-arguments