Skip to content
gitonthescene edited this page Feb 26, 2022 · 11 revisions

&x x:int takes a list of ints and returns the ints from 0 to (length of x-1), each repeated as many times as x[i].

Some examples may make this clearer:

  &1 2 3 4 5
0 1 1 2 2 2 3 3 3 3 4 4 4 4 4
  &5 4 3 2 1          / 5 0s, 4 1s, 3 2s, 2 3s, 1 4
0 0 0 0 0 1 1 1 1 2 2 2 3 3 4
  &0 0 1 0 0 1 1 0    / where are the 1s?
2 5 6
  (!10)!2             / 0 to 9 modulus 2
0 1 0 1 0 1 0 1 0 1
  &0=(!10)!2          / where are the 1s in 0 to 9 modulus 2: filter even numbers
0 2 4 6 8
  sparse:{((#x)#0 1)[&x]}  / index repeating 0 1 by (where x)
{((#x)#0 1)[&x]}
  sparse 10 2 35 1 8 1 9   / 10 0s, 2 1s, 35 0s, 1 1, 8 0s, 1 1, 9 0s
0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0

One way to look at verb & is by using the following histogram example:

  h:1 2 3 2 1  /we throw 9 balls into 5 boxes: 1 ball fell in box 0 etc
1 2 3 2 1
  &h  /we tag each ball with the box number (starting with 0) and line them up
0 1 1 2 2 2 3 3 4
  #:'=0 1 1 2 2 2 3 3 4  /put them back into boxes
1 2 3 2 1

Note though that this relies on there being at least one ball in each bucket.

  h:1 2 3 2 1
  (h;#:'=&h)
(1 2 3 2 1
 1 2 3 2 1)
  h:0 2 3 2 1
  (h;#:'=&h)
(0 2 3 2 1
 2 3 2 1)

Here is a more complete histogram than #:'=.

  h:0 2 3 2 1
  hist:{k:x@*:'g:=x;@[&1+|/k;k;:;#:'g]}
  (h;hist@&h)
(0 2 3 2 1
 0 2 3 2 1)