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

sourceval 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:

sourceval size  = 4
def in    = ArithmSeq(length = size)
val a     = RepeatWindow(in, 1   , size)
val b     = RepeatWindow(in, size, size)
val guard = a < b
RunningSum(guard).last.poll("num-pairs")
val aF    = FilterSeq(a, guard)
val bF    = FilterSeq(b, guard)
aF.poll(DC(1), "A")
bF.poll(DC(1), "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).

The source code for this page can be found here.