Filtering lists in an efficient way

Suppose you provide two things to a function I will describe in a moment:

  1. the # of branches of the union-type (in your example, 4)
  2. a function from the type to integers 0…3 (in general, to 0…(n-1) where n is the value in #rfc1951

Then a function that allocates an array (of length #1) of lists of type a, and iterates down the list, calling function #1 on each value, and using that to drop the value into the selected slot in the array, should suffice.
At the end, you could List.rev each slot in the array, just for neatness.

1 Like