2

假设我有

o: context [

  f: func[message /refine message2][
    print [message] 
    if refine [print message 2]
  ]

]

我可以这样称呼它

do get in o 'f "hello"

但是我该怎么做才能细化呢?像这样的东西

>> do get in o 'f/refine "hello" "world"
** Script Error: in expected word argument of type: any-word
** Near: do get in o 'f/refine
>>
4

3 回答 3

2

我不知道是否有办法直接告诉解释器在调用函数 value时使用细化。这将需要一些参数化,do当它的参数是一个function! 没有类似的东西似乎存在......但它可能隐藏在其他地方。

我知道使用细化的唯一方法是使用路径。为了清楚起见,我将首先使用一个临时词:

>> fword: get in o 'f
>> do compose [(to-path [fword refine]) "hello" "world"]  
hello
world

撰写后第二条语句的计算结果是:

do [fword/refine "hello" "world"]

您实际上也可以将函数值放入路径中。它摆脱了对中介的需求:

>> do compose [(to-path compose [(get in o 'f) refine]) "hello" "world"]
hello
world

messagePS你之间和上面有一个额外的空间 2,它应该是message2

于 2009-12-19T21:26:32.027 回答
1

做这个:

o/('f)/refine "hello" "world"

如果路径表达式中的括号对应于对象字段或系列拾取/戳索引引用,则评估它们。这使得上面的代码等价于:

apply get in o 'f ["hello" true "world"]

请注意,apply参数是位置性的,因此您需要知道声明参数的顺序。您不能使用函数优化本身来做到这一点,因此您必须使用apply或创建路径表达式来评估是否要对优化进行参数化的函数调用。

于 2013-10-03T17:26:51.700 回答
-1

使用简单的路径 o/f/refine

于 2009-12-17T11:20:28.430 回答