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: Jan-Peter Voigt
Subject: Re: Function template (or whatever it's called)
Date: Wed, 20 Jun 2018 11:48:02 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0

Hi Urs,

just one pointer on Davids message (thank you!): in my example the code has to be a singular expression, which is the case in most of my cases where I use let or begin. If you want to use more than one statement use the optional-argument-dot (define-my-custom-function . code) and unquote splice ,@code

Jan-Peter

Am 20.06.2018 um 11:42 schrieb Urs Liska:
Hi Jan-Peter,

thanks for the quick response!


Am 20.06.2018 um 11:35 schrieb Jan-Peter Voigt:
Hi Urs,

define-macro is the method you are looking for:

Indeed, that's what I need.
I might have managed to look that up myself, but I think that with the quoting stuff in place it would have driven me crazy before I'd reached anything ;-)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\version "2.19.80"

% define macro
#(define-macro (define-my-custom-function code)
    ; all wrapping code is (semi)quoted
   `(define-music-function
     (mus)(ly:music?)
     (let*
      ((elts (ly:music-property mus 'elements))
       (cnt (length elts)))

      ,code ; insert (unquoted) user generated code

      mus)))


% use macro
myFunc =
#(define-my-custom-function
   (ly:message "The music has ~a elements" cnt))

% use function
{
  \myFunc { c' e' }
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

HTH

It does, and I think I can go from here myself.

Thanks
Urs

Jan-Peter

Am 20.06.2018 um 11:18 schrieb Urs Liska:
I'm providing users with the option to configure some behaviour by providing custom styling functions. These functions must have a certain interface and will typically retrieve the same information from their arguments. I would like to simplify this task by providing a "function template". I think this may be achieved by macros or some other syntactic extension mechansim, but I have no idea about how to go forward with that.

This is what currently a user would have to do:

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

Is this possible? I would assume so, and I'd also would assume it's quite easy - once you know what to look for.

Thanks
Urs


_______________________________________________
lilypond-user mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/lilypond-user


_______________________________________________
lilypond-user mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/lilypond-user


_______________________________________________
lilypond-user mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/lilypond-user




reply via email to

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