Understanding J From
In J:
a =: 2 3 $ 1 2 3 4 5 6
gives:
1 2 3
4 5 6
This is an array 2 3
.
If I do this:
0 1 { a
I (noting which 0 1
is a 2
figurative list) expected back:
1 2 3 4 5 6
But instead it turned out like this:
1 2 3
4 5 6
Reading the documentation I expected the index shape to match the response shape.
Can someone clarify what I am missing here?
source to share
Larger arrays can help make this clear. A dimensioned array has dimensioned n
items n-1
. When you select an element from ( {
) a 3D array, your result is a 2D array:
1 { i. 5 3 4
12 13 14 15
16 17 18 19
20 21 22 23
When you select multiple items from an array, the items are collected into a new array, using each atom x
to select an item y
. This may be when you realized that the shape x
affects the shape of the result.
2 1 0 2 { 'set'
test
$ 2 1 0 2
4
$ 'test'
4
The dimensions of the result are equal to the dimensions x
plus the dimensions of the elements y
. So, if you have a 2D x
translating 2D elements from 3D y
, you will have a 4D result:
(2 2 $ 1 1 0 1) { i. 5 3 4
12 13 14 15
16 17 18 19
20 21 22 23
12 13 14 15
16 17 18 19
20 21 22 23
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
16 17 18 19
20 21 22 23
$ (2 2 $ 1 1 0 1) { i. 5 3 4
2 2 3 4
One final note: the monadic Ravel ( ,
) will reduce the result to a list (one dimensional array).
, 0 1 { 2 3 $ 1 2 3 4 5 6
1 2 3 4 5 6
, i. 2 2 2 2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
source to share
From ( {
) selects elements of a noun. For 2 3 $ 1 2 3 4 5 6
elements, they are two strings, because elements are the components that make up the noun.
[ a=. 2 3 $ 1 2 3 4 5
1 2 3
4 5 1
0 { a
1 2 3
If you only have 1 2 3
, then the elements will be separate atoms.
[ b=. 1 2 3
1 2 3
0 { b
1
If you used 1 3 $ 1 2 3
then there is only one item and the result will be
[ c=. 1 3 $ 1 2 3
1 2 3
0 { c
1 2 3
The number of items can be found using Tally ( #
) and is the base size of the $
noun form ( ).
$ a
2 3
$ b
3
$ c
1 3
# a
2
# b
3
# c
1
source to share