unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: "Linus Björnstam" <linus.internet@fastmail.se>
To: "Jean Abou Samra" <jean@abou-samra.fr>, guile-user@gnu.org
Subject: Re: Macro to prepend element to list
Date: Sat, 20 Mar 2021 19:10:06 +0100	[thread overview]
Message-ID: <40b6ccf5-d3ec-406c-b05c-bf55ebc011b5@www.fastmail.com> (raw)
In-Reply-To: <92e5fb1a-a57b-b65c-28a6-f6d3ef397fb2@abou-samra.fr>

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...

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...

-- 
  Linus Björnstam

On Sat, 20 Mar 2021, at 18:05, Jean Abou Samra wrote:
> 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
> 
> 
> Attachments:
> * prepend-example.ly
> * prepend-example.pdf



  reply	other threads:[~2021-03-20 18:10 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 [this message]
2021-03-20 20:16       ` Jean Abou Samra

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=40b6ccf5-d3ec-406c-b05c-bf55ebc011b5@www.fastmail.com \
    --to=linus.internet@fastmail.se \
    --cc=guile-user@gnu.org \
    --cc=jean@abou-samra.fr \
    /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).