unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Jean Abou Samra <jean@abou-samra.fr>
To: "Linus Björnstam" <linus.internet@fastmail.se>, guile-user@gnu.org
Subject: Re: Macro to prepend element to list
Date: Sat, 20 Mar 2021 21:16:14 +0100	[thread overview]
Message-ID: <9e46f19e-d66d-0009-2fd8-aebace21c3b9@abou-samra.fr> (raw)
In-Reply-To: <40b6ccf5-d3ec-406c-b05c-bf55ebc011b5@www.fastmail.com>

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




      reply	other threads:[~2021-03-20 20:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-20 14:24 Macro to prepend element to list Jean Abou Samra
2021-03-20 16:04 ` Linus Björnstam
2021-03-20 17:05   ` Jean Abou Samra
2021-03-20 18:10     ` Linus Björnstam
2021-03-20 20:16       ` Jean Abou Samra [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9e46f19e-d66d-0009-2fd8-aebace21c3b9@abou-samra.fr \
    --to=jean@abou-samra.fr \
    --cc=guile-user@gnu.org \
    --cc=linus.internet@fastmail.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).