revOn (subdivide a pattern and reverse the order of its parts)

Here's a function I made that quickly creates a lot of variation for patterns, specially when used in its list form. This function basically takes a number, divides the pattern (or rather, the current cycle) into that amount of pieces, and then reverses the order of those pieces. The list form allows you to easily do this concatenatively.

revOn' vs f = case vs of
    []     -> f
    (x:xs) -> inside x rev $ rev $ revOn' xs f

revOn x = revOn' [x]

drawLine $ rev $ "0..7"
-- |76543210|76543210

drawLine $ revOn 2 $ "0..7"
-- |45670123|45670123

drawLine $ revOn 4 $ "0..7"
-- |67452301|67452301

drawLine $ revOn' [2,4] $ "0..7"
-- |23016745|23016745

The divisor can be fractional and can even be patterned, be it as part of the list or not. Which makes it super easy to create super scrambled, complex variations of patterns that are not random.

do
  d1 $ revOn' [8,0.5,2,"<1 4>"] $ note (scale "phrygian" "0..7") # s "arpy" # room 0.3
  d2 $ "bd"

Strudel version:

// will add soon
8 Likes

Yes. Just as a code-golfing exercise, and advertisment for higher-order functions, the following is equivalent:

revOn' vs f = foldr (\x -> inside x rev . rev) f vs

And just for fun: revOn' [2] is rotation by 1/2,

tidal> drawLine $ revOn' [2] "0..7"
|45670123|...
tidal> drawLine $ (1/2) ~> "0..7"
|45670123|...

and this also works if the split is uneven (two parts, different lengths), and this is actually used in a Java standard library: https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/Collections.java#L843

1 Like