The J programming language is a successor of APL (see http://www.jsoftware.com). These languages are made for processing arrays of data and are able to express complex notions of iteration implicitly.
The following are some concepts borrowed from or inspired by J. Thinking about multidimensional arrays can be both mind bending and mind expanding. It may take some effort to grasp what is happening in these examples.
iota fills an array with a counter
fill a multidimensional array
using dup to create arrays
! is an abbreviation of dup
other ways to do the same thing:
Adverbs are a third argument passed to binary operators that modifies how they iterate over SequenceableCollections or Streams. See the Adverbs for Binary Operators help file for more information.
J has a concept called verb rank, which is probably too complex to understand and implement in SC, but operator depth is similar and simpler. A binary operator can be given a depth at which to operate. Negative depths iterate the opposite operand. These are better understood by example. It is not currently possible to combine adverb and depth.
deepCollect operates a function at different dimensions or depths in an array.
slice can get sections of multidimensional arrays. nil gets all the indices of a dimension.
generate a random array:
order returns an array of indices representing what would be the sorted order of the array:
calling order on the order returns an array of indices that returns the sorted array to the original scrambled order:
bubbling wraps an item in an array of one element. it takes the depth and levels as arguments.
similarly, unbubble unwraps an Array if it contains a single element.
the +++ operator takes each item from the second list and appends it to the corresponding item in the first list. If the second list is shorter, it wraps.
reshapeLike allows you to make one nested array be restructured in the same manner as another.
If the lengths are different, the default behaviour is to wrap:
but you can specify other index operators:
maxSizeAtDepth allows you to check the maximum array size at a given depth dimension
flopDeep allows you to to invert the outermost dimension with a dimension at any depth. This is analogous to flop, which does the same for 2-dimensional arrays.
allTuples will generate all combinations of the sub arrays