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]

(複数の) 引数でこれを機能させるにはどうすればよいですか?

ここには 2 つの可能性があります。1つは適用です:

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

もう 1 つは、ブロックを作成して実行することです。

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

注意: APPLY は風変わりな可能性があり、まだ Red にはないと思います。実験する価値があります。

于 2016-10-19T15:45:06.637 に答える