]> git.donarmstrong.com Git - lilypond.git/commitdiff
5067: Add ly:version? and lexicographic-list-compare?
authorUrs Liska <ul@openlilylib.org>
Tue, 21 Feb 2017 11:42:48 +0000 (12:42 +0100)
committerUrs Liska <ul@openlilylib.org>
Sun, 26 Feb 2017 19:15:38 +0000 (20:15 +0100)
Squashed commits:
5067: Restrict ly:version? to number list
5067: version-list-compare? rename to lexicographic-list-compare?
5067: version-list-compare? typo
ly:version? and version-list-compare?

A lexicographic list comparison for arbitrary element types,
but with special treatment of trailing elements
(additional elements of the longer list are ignored, which is
typical for version comparisons such as LilyPond 2.19 is equal to 2.19.4)
Simplify lilypond-version operator and create new interface
Add lilypond version predicates/operators

This set of predicates/operators compares a given reference version
to the LilyPond version that is currently being executed.
This makes it possible to implement "version switches" to write
(library) code that is compatible over syntax changes.

scm/lily-library.scm

index 214c095cf2c720c0429cb8dfc6b753acc1304b77..c6f066ca3284918723d1be4b8a859d8801f2c2a8 100644 (file)
@@ -888,6 +888,29 @@ and will be applied to NUM."
     (fancy-format #f (car custom-format) num))
    (else (fancy-format #f "~(~@r~)" num))))
 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; lilypond version
+
+(define (lexicographic-list-compare? op a b)
+  "Lexicographically compare two lists @var{a} and @var{b} using
+   the operator @var{op}. The types of the list elements have to
+   be comparable with @var{op}. If the lists are of different length
+   the trailing elements of the longer list are ignored."
+  (let* ((ca (car a))
+         (iseql (op ca ca)))
+    (let loop ((ca ca) (cb (car b)) (a (cdr a)) (b (cdr b)))
+      (let ((axb (op ca cb)))
+        (if (and (pair? a) (pair? b)
+                 (eq? axb iseql (op cb ca)))
+            (loop (car a) (car b) (cdr a) (cdr b))
+            axb)))))
+
+(define (ly:version? op ver)
+  "Using the operator @var{op} compare the currently executed LilyPond
+   version with a given version @var{ver} which is passed as a list of 
+   numbers."
+  (lexicographic-list-compare? op (ly:version) ver))
+
 ;;;;;;;;;;;;;;;;
 ;; other