# Remove boxes under preservation of shape

This challenge is specific to languages of the APL family. Although it might be solvable in other languages, please do not expect this to be easy as it depends on some specific concepts of this language family.

Many array oriented languages have the concept of a box (also called enclosure). A box encapsulates an array, hiding its shape. Here is an example of a boxed array:

┌─┬───────┬─────┐
│0│1 2 3 4│0 1 2│
│ │       │3 4 5│
└─┴───────┴─────┘


This array was generated from the J sentence 0 ; 1 2 3 4 ; i. 2 3 and contains three boxes, the first containing the scalar 0, the second containing a vector of shape 4, and the third containing a matrix of shape 2 3.

Some arrays of boxes look like they are plain arrays broken apart by boxes:

┌─┬───────────┐
│0│1 2 3 4    │
├─┼───────────┤
│5│ 9 10 11 12│
│6│13 14 15 16│
│7│17 18 19 20│
│8│21 22 23 24│
└─┴───────────┘


The previous array was generated from the J expression 2 2 $0 ; 1 2 3 4 ; (,. 5 6 7 8) ; 9 + i. 4 4. Your goal in this challenge is to take an array like that and remove the boxes separating the subarrays from one another. For instance, the previous array would be transformed into 0 1 2 3 4 5 9 10 11 12 6 13 14 15 16 7 17 18 19 20 8 21 22 23 24  Submit a solution as a monadic verb / function. The verb must work on arrays of arbitrary rank. You may assume that the shapes of the boxed arrays fit, that is, boxed subarrays adjacent on an axis have to have the same dimensions in all but that axis. Behaviour is undefined if they don't. This challenge is code golf. The submission compromising the least amount of characters wins. • Which languages are considered part of the APL family? – Alex A. Apr 1 '15 at 15:37 • @AlexA. Languages like APL, J, and K. – FUZxxl Apr 1 '15 at 15:37 • @randomra An array of boxes such that the arrays in the boxes if concatenated along the respective axes would form one large array. Imagine each boxed subarray to be a cuboid or rectangle and you put these together as specified by the boxes' positions to get a large cuboid or rectangle. – FUZxxl Apr 1 '15 at 15:46 • @randomra Your input would be considered invalid as the subarrays are not directly concatenable along the axes they would be concatenated. – FUZxxl Apr 1 '15 at 15:51 • In the interest of making this less language-specific, why not provide the example also using simple nested array notation for other languages? – Martin Ender Apr 1 '15 at 18:43 ## 2 Answers # J, 73 chars f=.3 :0 >".'z',~,(',"','&.>/',~":)"*1+i.#$z=.(,$~$,~1#~(#$y)-#@$)&.>y
)


Usage and tests:

   a=.2 2 $0 ; 1 2 3 4 ; (,. 5 6 7 8) ; 9 + i. 4 4 f a 0 1 2 3 4 5 9 10 11 12 6 13 14 15 16 7 17 18 19 20 8 21 22 23 24 f a,:a NB. rank 3 test 0 1 2 3 4 5 9 10 11 12 6 13 14 15 16 7 17 18 19 20 8 21 22 23 24 0 1 2 3 4 5 9 10 11 12 6 13 14 15 16 7 17 18 19 20 8 21 22 23 24  Golfing and explanation coming tomorrow. • Your solutions looks like you took a sledgehammer to the problem and beat it with brute force until it ceased to exist. – FUZxxl Apr 1 '15 at 18:43 • @FUZxxl Exactly. Interested in nice solutions. – randomra Apr 1 '15 at 18:47 # J, 39 f=:([:;(([:(;"1@|:)(;/&> ::]))&.>)@<"1)  Usage and tests a=.2 2$ 0 ; 1 2 3 4 ; (,. 5 6 7 8) ; 9 + i. 4 4
f a
0  1  2  3  4
5  9 10 11 12
6 13 14 15 16
7 17 18 19 20
8 21 22 23 24

• Is the name for v really needed? This csn be shortened I think. – FUZxxl Apr 1 '15 at 20:33
• good point. shortened – joebo Apr 1 '15 at 20:37
• You can leave out some parentheses: [:;([:;"1@|:;/&> ::])&.>@<"1 it's not needed to assign the verb to a name either. – FUZxxl Apr 2 '15 at 10:45
• This looks a little bit like it would only work with two-dimensional test-cases. – FUZxxl Apr 2 '15 at 10:46