alda-lang / alda-core

The core machinery of Alda
80 stars 26 forks source link

Sequence reversal operator #7

Closed daveyarwood closed 7 years ago

daveyarwood commented 7 years ago

Moved from https://github.com/alda-lang/alda/issues/130.

Quoting myself:

Per @MillerMark in #7:

Sequence reversals are indicated with the "!" operator and reverse the sequence of all the notes held in a variable or note group.

Examples:

piano: [a b >c]!

runUp = o4 c8. d e f g
runDown = runUp!
updown = runUp a runDown <b
piano: updown*2

Opening this idea up for discussion.

My 2¢: My immediate thought is that this idea may be difficult to implement in practice, based on the way Alda's event system works. Each syntactical element translates into an event which mutates the state of an Alda score, and this happens sequentially as the evaluator steps through the score. It seems like it would be difficult to implement a reliable "run this code backwards" function... which makes me shy away from wanting to support this officially. But, this could be a good use case for a plugin ( #37 ).

daveyarwood commented 7 years ago

I'm still leaning against adding this as a syntactic feature.

Inline Clojure code has matured since this issue was created, and I think reversing a sequence is a good example of one of the cool things you can do with it:

piano:
  (reverse (alda-code "c8 d e f g"))

Notably, this doesn't behave exactly like you might expect -- you end up getting quarter notes G, F, E, D followed by an eighth note C.

But, you could get around that by making your code more explicit:

piano:
  (set-duration (note-length 8))
  (reverse (alda-code "c d e f g"))

The above gives you 8th notes G-F-E-D-C.

One overall goal in my design decisions for Alda is to make the language as simple as possible. Each new proposed operator makes the language more complicated. I would prefer to do things with inline Clojure code wherever possible.