lilypond-user
[Top][All Lists]
Advanced

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

Re: ‘new text-spanner’ development


From: David Kastrup
Subject: Re: ‘new text-spanner’ development
Date: Wed, 23 Sep 2015 13:54:39 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Simon Albrecht <address@hidden> writes:

> Hello,
>
> as the other thread was becoming a monster, I start up a new one.
> I’ve now got a version of text-span-spread.ly, as I call it, and it
> compiles now. However, a new problem appeared, which I assume has to
> do with variable scope and seems to be over my head: The first call to
> the \startTextSpan music function sets the texts for _all_ the
> subsequent calls. Even more suspiciously, the tweak to font-shape in
> line 725 (ugh…) is also applied to all these instances.
> I’m sorry to call for help so soon again, but I just don’t have enough
> experience and background to troubleshoot this with any degree of
> efficiency. Any hints are welcome! :-)

Well, I don't know who is responsible for this particular idiom
encountered frequently in here, but the following open-coded loop is
both opaque and inefficient (namely O(n^2) as _appending_ to a list is
an O(n) operation):

extractLyricEventInfo =
#(define-scheme-function (lst) (ly:music?)
   "Given a music expression @var{lst}, return a list of pairs.  The
@code{car} of each pair is the text of any @code{LyricEvent}, and the
@code{cdr} is a boolean representing presence or absence of a hyphen
associated with that @code{LyricEvent}."
   ;; TODO: include duration info, skips?
   (let ((grist (extract-named-music lst '(LyricEvent))))
     (let mill ((grist grist) (flour '()))
       (if (null? grist)
           flour
           (let* ((text (ly:music-property (car grist) 'text))
                  (hyphen (extract-named-music (car grist) 'HyphenEvent))
                  (hyphen? (not (null? hyphen))))
             (mill (cdr grist)
               (append flour (list (cons text hyphen?)))))))))

Open-coded, you are much better off writing:

   (let ((grist (extract-named-music lst '(LyricEvent))))
     (let mill ((grist grist) (flour '()))
       (if (null? grist)
           (reverse! flour)
           (let* ((text (ly:music-property (car grist) 'text))
                  (hyphen (extract-named-music (car grist) 'HyphenEvent))
                  (hyphen? (not (null? hyphen))))
             (mill (cdr grist)
               (cons (cons text hyphen?) flour))))))

Since cons is O(1) and the final reverse! O(n) is only done once, you
arrive at O(n) for all.  But why open-code in the first place?

  (map (lambda (elt)
         (let* ((text (ly:music-property elt 'text))
                (hyphen (extract-named-music elt 'HyphenEvent))
                (hyphen? (pair? hyphen)))
            (cons text hyphen)))
       (extract-named-music lst 'LyricEvent)))

For something that is one-on-one, this is far more transparent.  And
even for something that is one-to-some (namely resulting in possibly 0,
1, or more elements), (append-map (lambda (elt) ...) ...) tends to be
much clearer.


-- 
David Kastrup



reply via email to

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