lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Function template (or whatever it's called)


From: David Kastrup
Subject: Re: Function template (or whatever it's called)
Date: Wed, 20 Jun 2018 11:38:35 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Urs Liska <address@hidden> writes:

> \version "2.19.80"
>
> myFunc =
> #(define-music-function (mus)(ly:music?)
>    (let*
>     ((elts (ly:music-property mus 'elements))
>      (cnt (length elts)))
>     (ly:message "The music has ~a elements" cnt)
>     mus))
>
> {
>   \myFunc { c' e' }
> }
>
> Create a music function with one ly:music? argument and extract some
> information from the music expression (of course this is just an
> example, in the real case it's a custom object attached to the music).
>
> What I would like to have is what define-music-function does here but
> with a pre-set argument list and essentially the let* bindings
> transparently done so that something like this is possible:
>
> userFunc =
> #(define-my-custom-function
>    (ly:message "The music has ~a elements" cnt))
>
> where one ly:music? argument is implicitly expected and the bindings
> like 'cnt' have already been prepared.

#(define-macro (define-my-custom-function . body)
  `(define-music-function (mus) (ly:music?)
    (let*
      ((elts (ly:music-property mus 'elements))
       (cnt (length elts)))
       ,@body
       mus)))

That's what one would call a pretty unhygienic macro since it messes
with a number of symbols/identifiers not specified by the user.

-- 
David Kastrup



reply via email to

[Prev in Thread] Current Thread [Next in Thread]