David Kastrup <d...@gnu.org> writes:

> stefano franchi <stefano.fran...@gmail.com> writes:
>
>> Is there a lilypond function that would allow me to extract a subset from a
>> pattern?
>>
>> For instance, let's say I have a simple A - natural scale and I want to get
>> the first five notes. Is there a function, call it \pick, that would allow
>> me to do something like:
>>
>> \scale = { a b c d e f g}
>> \pick 0 4 \scale
>>
>> and return {a b c d e} ?
>>
>> Could not find anything in the manuals, but I may be lacking the right
>> terms to search for.
>
> Here is an example implementation.  Negative numbers in the first
> element count from the end of the list, negative numbers in the second
> element indicate the elements to be removed from the right rather than
> taken from the left (-1 removes none, -2 removes 1, -3 removes 2:
> slightly awkward but 0 already means "leave no element" and so cannot
> simultaneously mean "remove no element").
>
> The music in question needs to actually be of a kind that has an
> 'elements field.

Well, _here_ is the example implementation.

pick =
#(define-music-function (a b m) (exact-integer? exact-integer? ly:music?)
  (set! (ly:music-property m 'elements)
   (let* ((l (ly:music-property m 'elements))
	  (t (list-tail l (if (negative? a) (+ (length l) a) a))))
    (list-head t (if (negative? b) (+ (length t) b 1) b))))
  m)

scale = { a b c d e f g}
\displayLilyMusic \pick 0 4 \scale
\displayLilyMusic \pick -2 -1 \scale
\displayLilyMusic \pick -3 -3 \scale

-- 
David Kastrup

Reply via email to