Vocabulary/bslash

From J Wiki
Jump to: navigation, search

>> <<   Down to: Dyad   Back to: Vocabulary Thru to: Dictionary

u\ y Prefix Adverb

Rank Infinity -- operates on x and y as a whole, by items of y -- WHY IS THIS IMPORTANT?



(u\y) applies verb u to successive prefixes of list y of increasing length (1, 2, 3, ...)

   ]\ 'banana'
b
ba
ban
bana
banan
banana

The main use is the form u/\

   +/\ 1 2 3 4 5 6  NB. compute running total
1 3 6 10 15 21

If you want to use u/\ y with a u that is not a primitive, consider using Suffix (\.) instead.


Common Uses

u\ y is magic when u is of the form u/. Look what happens then:

   u =: dyad define    NB. This verb shows what the operands of u are
x , ' u ' , y
)
   u/\ 'abcdef'
a
a u b
a u b u c
a u b u c u d
a u b u c u d u e
a u b u c u d u e u f

u gets applied between larger and larger prefixes of y. The effect is of a loop, when on each iteration one more item of y in operated on.

Let's look at a concrete example. Suppose u is +

   plus =: dyad define
x , ' + ' , y
)
   plus/\ 'abcdef'
a
a + b
a + b + c
a + b + c + d
a + b + c + d + e
a + b + c + d + e + f

Get the idea? The operand u is applied between longer and longer prefixes of y. It's a loop that reports the results of each iteration.

1. To make a running sum of the items of y

   +/\ 1 2 3 4 5
1 3 6 10 15

2. To create the running maximum, i.e. the maximum value encountered so far at each point in a list

   >./\ 3 1 4 1 5 9 2 6
3 3 4 4 5 9 9 9

3. To count the parity at each point in a list, i.e. whether the items up to and including each item contain an odd number of 1s

   ~:/\ 1 0 0 1 1 0 1 0 1 1 1 0 0 0
1 1 1 0 1 1 0 0 1 0 1 1 1 1

4. When y has exactly 2 items, u/\y is a good way to replace the second item with a function of y while leaving the first item unchanged.

Example: suppose the atoms of y represent the starting index of a substring and the ending index plus one. You can then convert easily to (start,length) form

   ]startend =: ; ('<td';'</td') I.@:E. each <'This is HTML.<td>It is table cells.</td>Extract one.'
13 35
   ]startlen =: -~/\ startend
13 22

More Information

1. Normally you will not use u/\ with non-primitive u unless you know that y is short. The reason can be seen by examining the order of evaluation of the successive prefixes

   u =: dyad define   NB. This verb shows the arguments of u
'(' , x , ' u ' , y , ')'
)
   u/\ 'abcdef'
a
(a u b)
(a u (b u c))
(a u (b u (c u d)))
(a u (b u (c u (d u e))))
(a u (b u (c u (d u (e u f)))))

The ugly fact disclosed here is that each row does not use the result of the previous row. The computation is not incremental at all: each row has to be computed separately, with the computation taking longer and longer as the prefix gets longer.

If J knew that u is associative, it could reorder the computation to be incremental. J knows it for primitives, hence those are fast. But, for more general u , J must assume the worst case.

To avoid this problem you should use Suffix (u\. y).

2. The first item of y is not changed by  u/\ y . (It could be changed by general u\ y) .


Details

1. u can be a cyclic gerund.

2. The argument to each invocation of u is a list of items of y (this has the same rank as y unless y is an atom).

3. If y has no items, the result of u\ y will also have no items.

However, u will be executed once on a 0-item cell to establish the shape of a result of u, and then the result of u\ y will be a list of 0 of that shape

   $ ]\ ''    NB. u is executed on cell of shape 0
0 0
   $ {.\ ''   NB. u produces an atomic result
0
   $ {.\ i. 0 3  NB. u is executed on cell of shape 0 3, returns shape 3
0 3
   $ ]\ i. 0 3   NB. u is executed on cell of shape 0 3, returns shape 0 3
0 0 3

Use These Combinations

Combinations using u\ y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Concatenated running totals on intervals (running total, but total is reset at start of each interval) ;@:(<@(f/\);.n) y

x ;@:(<@(f/\);.n) y
(f is any verb)

n e. _2 _1 1 2; also [: ; (f... also <@:(f/\); also <@:
Bitwise reduction and scan integer x (m b.)/ y

m is 16 to 31

/\ /\. in place of / much faster than alternatives
Create list of integers from 1 to # y, or the reverse #\ y also #\. which is the reverse faster than >:@i.@# y


x u\ y Infix Adverb

Rank 0 _ -- operates on atoms of x, and the entirety of y -- WHY IS THIS IMPORTANT?



(x u\y) applies verb u to successive parts of list y (called infixes).

1. If x is positive, u is applied to every infix of x items of y (the infixes may overlap)

   3 <\ 'abcdefg'
+---+---+---+---+---+
|abc|bcd|cde|def|efg|
+---+---+---+---+---+

2. If x is negative, the first infix comprises the first  |x items; subsequent infixes do not overlap but do include all items of y, which means that the last infix may be short

   _3 <\ 'abcdefg'
+---+---+-+
|abc|def|g|
+---+---+-+

Common uses

1. Reshape a list into a table of width 2

More convenient than ($) -- avoids discovering the length of the list.

   x=: _2         NB. width is 2, negate to get non-overlapping rows
   x ]\ 'abcde'   NB. here it pads with spaces
ab
cd
e
   x ]\ i.5       NB. here it pads with zeros
0 1
2 3
4 0
   x ]\ 'Fred';10;'John';20;'Eva';30   NB. can handle boxed list
+----+--+
|Fred|10|
+----+--+
|John|20|
+----+--+
|Eva |30|
+----+--+

Example: A handy utility to reshape into rows of width x (=2 by default)

   listx=: 2&$: :(] ]\~ [: - [)
   listx i.5   NB. use (:) and ($:) to default x to 2
0 1
2 3
4 0
   10 listx i.30
 0  1  2  3  4  5  6  7  8  9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29

2. Chop a list into boxes of pairs of items

(Same principle as 1. above, but replaces (]) with (<)

   x=: _2
   x <\ 'abcde'
+--+--+-+
|ab|cd|e|
+--+--+-+

3. Perform an operation on successive pairs of items

   x=: 2               NB. NOT negated because you want overlapping pairs
   x +/\ 3 1 4 1 5 9   NB. Sum successive pairings: (3 1), (1 4), (4 1), etc.
4 5 5 6 14

4. Chop a long string into a table that fits on the screen

   ]s =: 200 $ 'Lorem ipsum dolor sit amet, consectetur adipisicing elit,'
Lorem ipsum dolor sit amet, consectetur adipisicing elit,...
   _50 ]\ s
Lorem ipsum dolor sit amet, consectetur adipisicin
g elit,Lorem ipsum dolor sit amet, consectetur adi
pisicing elit,Lorem ipsum dolor sit amet, consecte
tur adipisicing elit,Lorem ipsum dolor sit amet, c

Details

1. u can be a cyclic gerund.

2. The argument to each invocation of u is a list of items of y (this has the same rank as y unless y is an atom).

3. If y has no items, or not enough items to create a single infix for a positive x, the result of  x u\ y will also have no items. However, u will be executed once on a cell of fills to establish the shape of a result of u, and then the result of  x u\ y will be a list of 0 of that shape. The shape of the cell of fills depends on whether x is positive or negative.

  • If x is positive, u is designed to see cells only with x items, and the cell of fills will have that shape
  • If x is negative, u must be prepared for cells of varying size, and the cell of fills will have 0 items
   $ _3 ]\ i. 0 3   NB. Negative x, fill cell is shape 0 3
0 0 3
   $ 3 ]\ i. 0 3   NB. Positive x, fill cell is shape 3 3
0 3 3

4. If x is zero, u is executed on an empty cell, (1+#y) times.


Use These Combinations

Combinations using x u\ y that have exceptionally good performance are shown in Special Combinations: Infixes.