3

How can I pass a function as an argument in red? Or would I not need that in red?

Using does I can define a function "with no arguments or local variables"

f: does [print 1] do f >> 1 

How can I make this work with (multiple) args? does is no the way, what is?

I want something like: (the following does NOT work):

; does NOT work f: does-with-args [x][print x] do f 23 >> 1 

In the last paragraph of this article http://blog.revolucent.net/2009/05/javascript-rebol.html the author says "allow functions to be passed as arguments" so I got excited, but it's also just using does :). But I learned it's possible.

1 Answer 1

3

How can I pass a function as an argument in red?

It doesn't seem this is the essence of your question, but you can pass a function as an argument in a couple of ways:

my-func: func [their-func [any-function!]][their-func "Stuff"] my-func :print my-func func [thing][probe uppercase thing] 

How can I make this work with (multiple) args?

There are two possibilities here. One is APPLY:

my-func: func [thing][print uppercase thing] apply :my-func ["Foo"] 

Another is to build up a block and DO it:

do collect [keep 'my-func keep "Bar"] do collect [keep :my-func keep "Baz"] ; keeps the function itself 
Sign up to request clarification or add additional context in comments.

1 Comment

apply is now fully supported in Red, adding many new features. Use ? apply from the console to see its specification. See the related blog entry for more info.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.