Rebol apply in Red 有什么等价物(如果有的话)?
What's the equivalent of Rebol apply in Red if any?
Rebol 已应用 Rebol apply in Red 有什么等价物?
目前,Red 中没有原生 apply
。可以自己写apply
:
apply: func [
"Apply a function to a block of arguments"
fn [any-function!] "Function value to apply"
args [block!] "Block of arguments (to quote refinement use QUOTE keyword)"
/local refs vals val
][
refs: copy []
vals: copy []
set-val: [set val skip (append/only vals val)]
parse args [
some [
'quote set-val
| set val refinement! (append refs to word! val)
| set-val
]
]
do compose [(make path! head insert refs 'fn) (vals)]
]
它的工作方式与 Rebol 的 apply
有点不同(因为我不喜欢 Rebol 的 apply
语法)。如果你定义了一些函数:
f: func [
foo
/bar
baz
][
reduce [foo bar baz]
]
那么这里是如何使用这个 apply
:
>> apply :f [1 /bar 1]
== [1 true 1]
>> apply :f [quote /bar]
== [/bar false none]
>> apply :f [quote /bar /bar 1]
== [/bar true 1]
Rebol 已应用
目前,Red 中没有原生 apply
。可以自己写apply
:
apply: func [
"Apply a function to a block of arguments"
fn [any-function!] "Function value to apply"
args [block!] "Block of arguments (to quote refinement use QUOTE keyword)"
/local refs vals val
][
refs: copy []
vals: copy []
set-val: [set val skip (append/only vals val)]
parse args [
some [
'quote set-val
| set val refinement! (append refs to word! val)
| set-val
]
]
do compose [(make path! head insert refs 'fn) (vals)]
]
它的工作方式与 Rebol 的 apply
有点不同(因为我不喜欢 Rebol 的 apply
语法)。如果你定义了一些函数:
f: func [
foo
/bar
baz
][
reduce [foo bar baz]
]
那么这里是如何使用这个 apply
:
>> apply :f [1 /bar 1]
== [1 true 1]
>> apply :f [quote /bar]
== [/bar false none]
>> apply :f [quote /bar /bar 1]
== [/bar true 1]