[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Macro to prepend element to list
From: |
Jean Abou Samra |
Subject: |
Re: Macro to prepend element to list |
Date: |
Sat, 20 Mar 2021 18:05:04 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 |
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
prepend-example.ly
Description: Text Data
prepend-example.pdf
Description: Adobe PDF document