6

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.

Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107
lucas1000001
  • 2,740
  • 1
  • 25
  • 22

2 Answers2

3

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.

lucas1000001
  • 2,740
  • 1
  • 25
  • 22
3

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 )
AndyG
  • 39,700
  • 8
  • 109
  • 143
draegtun
  • 22,441
  • 5
  • 48
  • 71
  • That's fairly useful. Do you know how to apply a dynamic list of arguments to a block of unknown arity? – mcandre Oct 12 '11 at 17:11
  • @mcandre: Sorry only just seen your comment. Not 100% sure what you're after because above may provide what you're looking for? I've created a more concise gist which will hopefully help: https://gist.github.com/1334070 – draegtun Nov 02 '11 at 16:20
  • 1
    Steve Dekorte [gets it](https://github.com/mcandre/io/commit/6ee2f4a0349cebb5365d0fbfb2259b2f4b62022b). :) And here's an [example](https://github.com/mcandre/iocheck) of its use. – mcandre Nov 02 '11 at 17:15