Arbitrary conditionals

Is it possible to use an arbitrary test function to decide whether or not to apply another function? For example, to determine if a MIDI control pattern satisfies some condition?:

-- test if midi control input #64 is greater than 0.3
-- if true, then apply `rev`
d1 $ ifArbitrary ((> 0.3) . (cF 0 "64")) rev $ s "bd drum*3 odx future*4"

-- or more plainly:
d1 $ ifArbitrary ((> 0.3) . ("0.1 0.7 0.6 0.2")) rev $ s "bd drum*3 odx future*4"

Apologies for my invalid Haskell. Hopefully the question makes sense.

Basically I want something like while, but not for binary patterns.

i think you're looking for fixRange

1 Like

You can just use while, using <$> to map the test over the pattern of numbers, therefore turning it into a binary pattern:

d1 $ while ((> 0.3) <$> (cF 0 "64")) rev $ s "bd drum*3 odx future*4"

This is awesome! :star_struck: I used fixRange sometimes, but this while version is way more handsome!

fmap for the win all day every day. :smiley:

While I personally don’t use it as often, don’t forget it’s relative <&> if you prefer how the following reads:

d1 $ while ((cF 0 "64") <&> (> 0.3)) rev $ s "bd drum*3 odx future*4"

It might also allow the dropping of some parens (though I’m away from my computer to double-check that)

d1 $ while (cF 0 "64" <&> (> 0.3)) rev $ s "bd drum*3 odx future*4"

In essence, <&> is to <$> as for is to fmap, and sometimes the latter pair will type/read better too, especially during partial application.

If you do this kind of thing a lot, you could probably go wild creating custom infix functions like <&<>, <&<=>, <&>>, <&>=>, and so on and remove yet more parens. :joy:

1 Like

Thanks everyone for the different strategies!

1 Like