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.

4

1 回答 1

3

如何将函数作为红色参数传递?

这似乎不是您问题的本质,但您可以通过以下几种方式将函数作为参数传递:

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

如何使用(多个)args 进行这项工作?

这里有两种可能。一种是应用:

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

另一个是建立一个块并执行它:

do collect [keep 'my-func keep "Bar"]
do collect [keep :my-func keep "Baz"] ; keeps the function itself

注意:APPLY 可能很古怪,我认为目前还没有红色,值得尝试。

于 2016-10-19T15:45:06.637 回答