[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 21:16:14 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 |
Le 20/03/2021 à 19:10, Linus Björnstam a écrit :
I see!
To be honest, this seems like a guile-1.8ism... I don't think such code would
work for guile2 though 3. Why? Because your are mutating a pointer local to
your procedure, not the actual data pointed to by the music property You are
modifying the pointer to the list returned by the call, not the list where it
is stored.
I don't know enough lilypond, but a more standard way of doing that would be something
like (ly:music-property-set! m 'articulations (cons (make-music 'ArticulationEvent
'articulation-type "staccato") (ly:music-property m 'articulations))) (or a
shorthand thereof).
More verbose, sure, but also standard scheme...
Well, not sure what you mean exactly, but ly:music-property is defined
with a setter:
(define-public ly:music-property
(make-procedure-with-setter ly:music-property
ly:music-set-property!))
which makes the code equivalent to (ly:music-set-property! m
'articulations (cons ... (ly:music-property m 'articulations))), doesn't
it?
This could of course be abstracted away in some nicer way, like something I
just pulled out of my posterior without much prior knowledge of lilypond
(except for a fingering chart for bassoon I maintain):
(update! (select '(note-event 'artuculations)) (lambda (p) (cons (make-music
...) m)))
This could be done with higher order functions and passing lambdas around
without having to rely on macros...
Indeed, we could do things along these lines:
\version "2.23.1"
#(define (ly:music-transform-property! music prop func)
(ly:music-set-property! music
prop
(func (ly:music-property music prop))))
addStaccato =
#(define-music-function (music) (ly:music?)
(map-some-music
(lambda (m)
(if (music-is-of-type? m 'note-event)
(ly:music-transform-property!
m
'articulations
(lambda (elts)
(cons (make-music 'ArticulationEvent 'articulation-type
"staccato")
elts))))
#f)
music))
\addStaccato { c'4 d' e'8 f' g' a' }
It is more verbose for prepending elements, but much more versatile, and
'cute' from (srfi srfi-26) can make the call more compact as well.
Best regards,
Jean