-->

Avoiding a repeated verb name in a train

2019-08-05 06:22发布

问题:

Consider a dyadic verb g, defined in terms of a dyadic verb f:

g=. [ f&.|: f

Is it possible to rewrite g so that the f term appears only once, but the behavior is unchanged?

UPDATE: Local Context

This question came up as part of my solution to this problem, which "expanding" a matrix in both directions like so:

Original Matrix

1 2 3
4 5 6
7 8 9

Expanded Matrix

1 1 1 1 2 3 3 3 3
1 1 1 1 2 3 3 3 3
1 1 1 1 2 3 3 3 3
1 1 1 1 2 3 3 3 3
4 4 4 4 5 6 6 6 6
7 7 7 7 8 9 9 9 9
7 7 7 7 8 9 9 9 9
7 7 7 7 8 9 9 9 9
7 7 7 7 8 9 9 9 9

My solution was to expand the matrix rows first using:

f=. ([ # ,:@{.@]) , ] , [ # ,:@{:@]

And then to apply that same solution under the transpose to expand the columns of the already row-expanded matrix:

3 ([ f&.|: f) m

And I noticed that it wasn't possible to write my solution with making the temporary verb f, or repeating its definition inline...

Try it online!

回答1:

Knowing the context helps. You can also approach this using (|:@f)^:(+: x) y. A tacit (and golfed) solution would be 0&(|:{.,],{:)~+:.

   (>: i. 3 3) (0&(|:{.,],{:)~+:) 2
1 1 1 2 3 3 3
1 1 1 2 3 3 3
1 1 1 2 3 3 3
4 4 4 5 6 6 6
7 7 7 8 9 9 9
7 7 7 8 9 9 9
7 7 7 8 9 9 9


回答2:

I don't think it is possible. The right tine is going to be the result of x f y and the left tine is x The middle tine will transpose and apply f to the arguments and then transpose the result back. If you take the right f out then there is not a way to have x f y and if the middle f is removed then you do not have f applied to the transpose.

My guess is that you are looking for a primitive that will accomplish the same result with only one mention of f, but I don't know of one.

Knowing the J community someone will prove me wrong!



标签: j