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 18:05:04 +0100	[thread overview]
Message-ID: <92e5fb1a-a57b-b65c-28a6-f6d3ef397fb2@abou-samra.fr> (raw)
In-Reply-To: <7fbe592f-2816-480d-86ca-3876277d2461@www.fastmail.com>

[-- Attachment #1: Type: text/plain, Size: 1293 bytes --]

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


[-- Attachment #2: prepend-example.ly --]
[-- Type: text/x-lilypond, Size: 435 bytes --]

\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 #3: prepend-example.pdf --]
[-- Type: application/pdf, Size: 27950 bytes --]

  reply	other threads:[~2021-03-20 17:05 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 [this message]
2021-03-20 18:10     ` Linus Björnstam
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=92e5fb1a-a57b-b65c-28a6-f6d3ef397fb2@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).