Abstracting boxed array structures in J
- by estanford
I've been working on a J function for a while, that's supposed to scan a list and put consecutive copies of an element into separate, concatenated boxes. My efforts have taken me as far as the function
(<;. 2) ((2&(~:/\)),1:)
which tests successive list entries for inequality, returns a list of boolean values, and cuts the list into boxes that end each time the number 1 appears. Here's an example application:
(<;. 2) ((2&(~:/\)),1:) 1 2 3 3 3 4 1 1 1
+-+-+-----+-+-----+
|1|1|0 0 1|1|0 0 1|
+-+-+-----+-+-----+
The task would be finished if I could then replace all those booleans with their corresponding values in the input argument. I've been looking for some kind of mystery function that would let me do something like
final =: mysteryfunction @ (<;. 2) ((2&(~:/\)),1:)
final 1 2 3 3 3 4 1 1 1
+-+-+-----+-+-----+
|1|2|3 3 3|4|1 1 1|
+-+-+-----+-+-----+
In an ideal situation, there would be some way to abstractly represent the nesting pattern generated by (<;. 2) ((2&(~:/\)),1:) to the original input list. (i.e. "This boxed array over here has the first element boxed at depth one, the second element boxed at depth one, the third, fourth, and fifth elements boxed together at depth one,..., so take that unboxed list over there and box it up the same way.") I tried fooling around with ;. , S: , L:, L. and &. to produce that behavior, but I haven't had much luck. Is there some kind of operator or principle I'm missing that could make this happen? It wouldn't surprise me if I were overthinking the whole issue, but I'm running out of ideas.