Avoiding a repeated verb name in a train
Asked Answered
C

2

1

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!

Credits answered 22/10, 2017 at 20:24 Comment(2)
Bob is right in the general case. If you give us more details and context on your specific case, there might be a local solution.Subscapular
@DanBron Thanks. I've updated the post.Credits
B
2

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
Barrens answered 24/10, 2017 at 0:8 Comment(5)
that golfed solution is beautiful, but i can't figure out how you mapped the power ^: solution down to it. It parses like this, which would mean that it's equivalent to 4 (0&(|:{.,],{:)) m, but since 0 is already bonded as the left arg to the verb on its right, I don't see where the 4 comes into play or how it produces the final result.Credits
@Credits Let m be the matrix and n be the number of times that matrix is expanded. A breakdown for it is m (0&(|:{.,],{:)~+:) n to m 0&(|:{.,],{:)~ (+: n) to (+: n) 0&(|:{.,],{:) m to (0&(|:{.,],{:))^:(+: n) mBarrens
makes sense until the very last step: what rule is being applied there?Credits
@Credits It's another rule for bond & where a noun is bonded to a dyad, and then called dyadically. For example, x m&f y becomes m&f^:x yBarrens
ah, ok that makes sense i think genuinely didn't know about that one, or had at least thoroughly forgotten it. great solution. you should post it.Credits
C
1

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!

Carollcarolle answered 23/10, 2017 at 2:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.