1 title:: J concepts in SC
2 summary:: An overview of concepts borrowed from J
5 The J programming language is a successor of APL. (http://www.jsoftware.com)
6 These languages are made for processing arrays of data and are able to express
7 complex notions of iteration implicitly.
9 The following are some concepts borrowed from or inspired by J.
10 Thinking about multidimensional arrays can be both mind bending and mind expanding.
11 It may take some effort to grasp what is happening in these examples.
13 section:: Filling arrays
14 iota fills an array with a counter
16 z = Array.iota(2, 3, 3);
17 z.rank; // 3 dimensions
18 z.shape; // gives the sizes of the dimensions
19 z = z.reshape(3, 2, 3); // reshape changes the dimensions of an array
20 z.rank; // 3 dimensions
26 Array.fill2D(3,3,{1.0.rand.round(0.01)});
28 Array.fill2D(2,3,{|i,j| i@j});
33 Array.fill3D(2,2,2,{1.0.rand.round(0.01)});
35 Array.fill3D(2,2,2,{|i,j,k| `[i,j,k]});
38 section:: Creating arrays
39 using dup to create arrays
44 {100.rand} dup: 3 dup: 4;
45 {{100.rand} dup: 3} dup: 4;
46 {|i| i.squared} dup: 10;
47 {|i| i.nthPrime} dup: 10;
49 ! is an abbreviation of dup
57 {|i| i.nthPrime} ! 10;
60 other ways to do the same thing:
62 // partial application
66 // operating on a list
71 section:: Operator adverbs
72 Adverbs are a third argument passed to binary operators that modifies how they iterate over
73 SequenceableCollections or Streams. See the link::Reference/Adverbs:: help file for more information.
75 [10, 20, 30, 40, 50] + [1, 2, 3]; // normal
76 [10, 20, 30, 40, 50] +.f [1, 2, 3]; // folded
77 [10, 20, 30, 40, 50] +.s [1, 2, 3]; // shorter
78 [10, 20, 30, 40, 50] +.x [1, 2, 3]; // cross
79 [10, 20, 30, 40, 50] +.t [1, 2, 3]; // table
82 section:: Operator depth
83 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.
84 A binary operator can be given a depth at which to operate. Negative depths iterate the opposite operand.
85 These are better understood by example. It is not currently possible to combine adverb and depth.
90 z +.0 y; // same as the above. y added to each row of z
91 z +.1 y; // y added to each column of z
92 z +.2 y; // y added to each element of z
93 z +.-1 y; // z added to each element of y
96 subsection:: deepCollect
97 deepCollect operates a function at different dimensions or depths in an array.
99 z = Array.iota(3, 2, 3);
100 f = {|item| item.reverse };
105 f = {|item| item.stutter };
111 section:: Sections of multidimensional arrays
112 slice can get sections of multidimensional arrays.
113 nil gets all the indices of a dimension.
116 z.slice(nil, (1..3));
118 z.slice((2..3), (0..2));
122 z = Array.iota(3,3,3);
123 z.slice([0,1],[1,2],[0,2]);
124 z.slice(nil,nil,[0,2]);
129 z.slice(nil,1,(1..2));
134 section:: Sorting order
135 generate a random array:
137 z = {100.rand}.dup(10);
139 order returns an array of indices representing what would be the sorted order of the array:
142 y = z[o]; // using the order as an index returns the sorted array
144 calling order on the order returns an array of indices that returns the sorted array to the
145 original scrambled order:
152 bubbling wraps an item in an array of one element. it takes the depth and levels as arguments.
162 similarly, unbubble unwraps an Array if it contains a single element.
170 [[4],[5]].unbubble(1);
172 z.bubble(1).unbubble(1);
173 z.bubble(2).unbubble(2);
176 section:: Laminating with the +++ operator
177 the +++ operator takes each item from the second list and appends it to the corresponding item
178 in the first list. If the second list is shorter, it wraps.
183 z +++ [[[77,88,99]]];
184 z +++ [ [[77]],[[88]],[[99]] ];
186 z +++ [77,88,99].bubble;
187 z +++ [77,88,99].bubble(0,2);
188 z +++ [77,88,99].bubble(1,2);
190 z +++ [11,22,33].pyramidg;
191 z +++ [11,22,33].pyramidg.bubble;
192 z +++ [[11,22,33].pyramidg];
193 z +++ [[[11,22,33].pyramidg]];
199 z.pyramid(i+1).postln;
200 z.pyramidg(i+1).postln;
206 section:: reshapeLike
207 reshapeLike allows you to make one nested array be restructured in the same manner as another.
209 a = [[10,20],[30, 40, 50], 60, 70, [80, 90]];
210 b = [[1, 2, [3, 4], [[5], 6], 7], 8, [[9]]];
214 If the lengths are different, the default behaviour is to wrap:
216 a = [[10,20],[30, 40, 50]];
217 b = [[1, 2, [3, 4], [[5], 6], 7], 8, [[9]]];
220 but you can specify other index operators:
222 a.reshapeLike(b, \foldAt);
224 a.reshapeLike(b, \clipAt);
226 a.reshapeLike(b, \at);
230 allTuples will generate all combinations of the sub arrays
232 [[1, 2, 3], [4, 5], 6].allTuples;
233 [[1, 2, 3], [4, 5, 6, 7], [8, 9]].allTuples;