[英]How can I define a verb in J that applies a different verb alternately to each atom in a list?
Imagine I've defined the following name in J: 想象一下,我在J中定义了以下名称:
m =: >: i. 2 4 5
This looks like the following: 如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
I want to create a monadic verb of rank 1 that applies to each list in this list of lists. 我想创建一个适用于此列表列表中每个列表的1级单子动词。 It will double ( +:
) or add 1 ( >:
) to each alternate item in the list. 它将加倍( +:
或将1( >:
:)添加到列表中的每个备用项目。 If we were to apply this verb to the first row, we'd get 2 3 6 5 10
. 如果将这个动词应用于第一行,我们将得到2 3 6 5 10
。
It's fairly easy to get a list of booleans which alternate with each item, eg, 0 1 $~{:$ m
gives us 0 1 0 1 0
. 获得与每个项目交替的布尔值列表是相当容易的,例如0 1 $~{:$ m
给我们0 1 0 1 0
。 I thought, aha! 我想, 啊哈! I'll use something like +:
` >: @.
我将使用+:
` >: @.
followed by some expression, but I could never quite get it to work. 后面加上一些表达,但我永远无法使它正常工作。
Any suggestions? 有什么建议么?
UPDATE UPDATE
The following appears to work, but perhaps it can be refactored into something more elegant by a J pro. 以下内容似乎可行,但J pro也许可以将其重构为更精美的内容。
poop =: monad define (($ y) $ 0 1 $~{:$ y) ((]+:)`(]>:) @. [)"0 y )
I would use the oblique verb, with rank 1 ( /."1
)- so it applies to successive elements of each list in turn. 我将使用倾斜动词,其等级为1( /."1
1)-,因此它依次适用于每个列表的连续元素。
You can pass a gerund into /.
您可以将gerund传递到/.
and it applies them in order, extending cyclically. 并按顺序应用它们,并循环扩展。
+:`>: /."1 m
2
3
6
5
10
12
8
16
10
20
22
13
26
15
30
32
18
36
20
40
42
23
46
25
50
52
28
56
30
60
62
33
66
35
70
72
38
76
40
80
(,@(+:`>:/。)“ 1 a)有效,但是请注意(((* 2 1 $〜$)@(+ 0 1 $〜$)” 1 a)也可以有效(并且是在我的简短测试中,在大型阵列上的速度要快20倍左右)。
I spent a long time and I looked at it, and I believe that I know why ,@
works to recover the shape of the argument. 我花了很长时间看了一下,我相信我知道为什么,@
可以恢复论证的形式。
The shape of the arguments to the parenthesized phrase is the shape of the argument passed to it on the right, even though the rank is altered by the "
conjugate (well, that is what trace called it, I thought it was an adverb). If ,
were monadic, it would be a ravel, and the result would be a vector or at least of a lower rank than the input, based on adverbs to ravel. That is what happens if you take the conjunction out - you get a vector. 括号词组的自变量的形状是在右侧传递给它的自变量的形状,即使其排名因"
共轭"
而改变了(嗯,这就是它所说的轨迹,我认为这是副词)。如果,
是单数的,那将是一个ravel,并且结果将是vector或至少比输入低的等级(基于ravel的副词)。 。
So what I believe is happening is that the conjunction is making ,
act like a dyadic ,
which is called an append. 因此,我认为正在发生的事情是,结合正在,
像一个二进,
这就是所谓的追加。 The append alters what it is appending to what it is appending to. 附加将附加内容更改为附加内容。 It is appending to nothing but that thing still has a shape, and so it ends up altering the intermediate vector back to the shape of the input. 它没有附加任何东西,但那个东西仍然具有形状,因此最终将中间向量更改回输入的形状。
Now I'm probably wrong. 现在我可能是错的。 But $,"0@(+:
>:/.)"1 >: i. 但是$,"0@(+:
>:/。)” 1>:i。 2 4 5 -> 2 4 5 1 1` which I thought sort of proved my case. 2 4 5-> 2 4 5 1 1`我认为这证明了我的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.