Ask Your Question

Patterns of patterns?

asked 2016-12-14 10:39:53 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >1 is welcome to improve it.

Is there a way I make a sort of sequencer by writing a pattern where each "thing" in it is another pattern rather than just a number/string?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-12-14 11:14:50 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >1 is welcome to improve it.

There are a few ways - which is best probably depends on what makes the most sense to you and is easiest to use for what you're trying to do.

But first let me review some Haskell syntax that will be helpful. You can make a definition that lasts just for a single expression like this:

let a = sound "bd sn"
    d1 $ a

This will define a, but only for that single expression (indentation is important here). What's also useful is that you send this from the editor (Atom: CTRL+Enter or Emacs: C-c C-e) as a single command.

using slowcat

OK, so now we can try doing patterns of patterns. It's not actually a pattern of patterns, but you can make a list of patterns with slowcat

let a = sound "bd sn"
    b = sound "cp cp"
    d1 $ slowcat [a, a, b, a]

using seqP

Another function that's not truly a pattern of patterns but still nice to know about is seqP. This lets you specify a specific time interval for each pattern

d1 $ seqP [ 
  (0, 2, sound "bd sn"), 
  (2, 3, sound "cp cp"), 
  (3, 4, sound "bd sn")

using do

To get a true pattern of patterns, one thing we can try is the do notation

let pp = [sound "gabba sn:2", sound "cp cp"]
    d1 $ do {n <- "{0 0 1 [0,1]}%1"; pp!!n}

What this does is take values from the pattern "{0 0 1 [0,1]}%1" and use them as the index for the list of patterns pp. Since do in Haskell can always be rewritten with >>=, this means exactly the same thing:

d1 $ "{0 0 1 [0,1]}%1" >>= \n -> [sound "gabba sn:2", sound "cp cp"]!!n

But I think it's harder to read.

using substruct

There's a nice Tidal function substruct, that lets you use a pattern as a subpattern inside another pattern. In the previous example I had two patterns that I was using inside another pattern, which just means I need to stack wo substruct's

d1 $ stack [
  substruct "{x x ~ x}%1" $ sound "gabba sn:2",
  substruct "{~ ~ x x}%1" $ sound "cp cp"

using fmap

There's yet another method that works a lilyttle different but in this case sounds the sam e in the end. Instead of making a pattern of list indices and pulling from those with do, we can "map" (or "lift") the index operator !! and apply it to the index pattern. One way of writing this:

d1 $ unwrap $ fmap ([sound "gabba sn:2", sound "cp cp"] !!) "{0 0 1 [0,1]}%1"

Since <$> is a synonym for fmap it's just a matter of personal preference if you want to write it this way instead:

d1 $ unwrap $ ([sound "gabba sn:2", sound "cp cp"] !!) <$> "{0 0 1 [0,1]}%1"

The reason for the unwrap is that the fmap expression really does create a pattern of patterns ... (more)

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-12-14 10:39:53 -0500

Seen: 100 times

Last updated: Dec 14 '16