Tips and Recipes for FScape

All-pairs sequences

The task is to generate the sequence of all possible (undirected) pairs from a given sequence, for example to match pairs of slices using the Slices UGen. In a plain Scala collection, if the input sequence were the numbers from zero until four, this would be

val size  = 4
val in    = 0 until size
val pairs = for (a <- in; b <- in if a < b) yield (a, b)

The resulting pairs being Seq((0,1), (0,2), (0,3), (1,2), (1,3), (2,3)). In FScape, we can emulate the looping over the input sequence using RepeatWindow, and the guard filter by using the FilterSeq UGen:

val size  = 4
def in    = ArithmSeq(0, length = size)
val a     = RepeatWindow(in, 1   , size)
val b     = RepeatWindow(in, size, size)
val guard = a < b
RunningSum(guard.toInt).last.poll("num-pairs")
val aF    = FilterSeq(a, guard)
val bF    = FilterSeq(b, guard)
aF.poll(true, "A")
bF.poll(true, "B")

The expected number of pairs is size * (size - 1) / 2, so six. If we group the values for A and B, we get the expected sequence (0,1), (0,2), (0,3), (1,2), (1,3), (2,3).