当前位置: 动力学知识库 > 问答 > 编程问答 >

j - Simple hook is not executed as expected

问题描述:

I define a as an array and find the running max:

 a =: 3 1 4 1 5 9 2

>./\ a

3 3 4 4 5 9 9

Then I want to filter out duplicates. I know that nub (~.) does this, so I try:

 ~. >./\ a

3 4 5 9

It works but I don't know why. I thought it should not work. / and \ are adverbs, so (>./\) is a verb. We then have: f g y, which is a hook, and it should be executed as y f (g y). Obviously it does not work that way.

Instead, it's executed as ~. (>./\) (i.e. f (g y)), like it was ~. @ (>./\). So what's going on here?

Thanks.

网友答案:

J executes statements right to left so your ~. >./\ a is equivalent to:

~. (((>./) \) a)

It would be a hook if it was parenthesized like:

(~.  ((>./) \)) a

Some relevant discussions: function composition and how to take the train.

Edit: To make this clearer, f g y is always f (g y). If you want the hook, you have to write (f g) y.

分享给朋友:
您可能感兴趣的文章:
随机阅读: