Le 20/03/2021 à 17:04, Linus Björnstam a écrit :

Well, mutating like that is not very common, except for maybe with alists.

In which situations are you mutating the list like that? Usually you would 
build a reverse list using a recursive function and an accumulator, which can 
be done without set! (which has a boxing overhead).


Mostly to mutate properties of LilyPond's probs (property objects).
This works somewhat like Guile's object properties. For example, to
add articulations on notes:

\version "2.23.1"

#(define-macro (prepend! thing lst)
   `(set! ,lst (cons ,thing ,lst)))

addStaccato =
#(define-music-function (music) (ly:music?)
   (map-some-music
     (lambda (m)
       (if (music-is-of-type? m 'note-event)
           (prepend! (make-music 'ArticulationEvent 'articulation-type "staccato")
                     (ly:music-property m 'articulations)))
       #f)
     music))

\addStaccato { c'4 d' e'8 f' g' a' }

(Output attached.)

It is also of use in so-called engravers (it's harder
to find a simple use case for these). I guess all of this
is so LilyPond-specific that it suits better in my personal
libraries, and maybe upstream if I see a compelling use
case in the code base.

Thanks!
Jean

\version "2.23.1"

#(define-macro (prepend! thing lst)
   `(set! ,lst (cons ,thing ,lst)))

addStaccato =
#(define-music-function (music) (ly:music?)
   (map-some-music
     (lambda (m)
       (if (music-is-of-type? m 'note-event)
           (prepend! (make-music 'ArticulationEvent 'articulation-type "staccato")
                     (ly:music-property m 'articulations)))
       #f)
     music))

\addStaccato { c'4 d' e'8 f' g' a' }

Attachment: prepend-example.pdf
Description: Adobe PDF document

Reply via email to