lilypond-devel
[Top][All Lists]
Advanced

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

Re: Simplify several library functions. (issue 7020044)


From: dak
Subject: Re: Simplify several library functions. (issue 7020044)
Date: Fri, 28 Dec 2012 15:48:56 +0000

Reviewers: lemzwerg, thomasmorley65,

Message:
On 2012/12/28 15:09:24, thomasmorley65 wrote:
LGTM

Two questions:
a) Where is `identity´ defined? I can't find it, neither in LilyPond
nor in the
guile-manual.

Huh.  The Guile2 manual shows its definition.  It's a common function
and is equivalent to (lambda (x) x).
I had not been aware that its documentation is deficient in Guile1.

b) Why are the definitions in lily-library.scm not all (or nearly all)
defined
public?
Some examples:
   split-list
   other-axis
   index-cell
   sign

I see no reason _not_ to make them public.

Actually, that would be the perfect reason to throw them out when they
are not being used in our current code base.  Apart from "other-axis",
they are not really specific to LilyPond and don't offer significant
advantages over the standard Guile libraries.

Description:
Simplify several library functions.

Most of those do not even seem to be used.  Maybe one should instead
remove them instead of having to maintain bit-rot prone stuff.

Simplify hash-table->alist, alist->hash-table, functional-or,
functional-and, list-element-index


Simplify first-member and first-assoc

Please review this at https://codereview.appspot.com/7020044/

Affected files:
  M scm/lily-library.scm


Index: scm/lily-library.scm
diff --git a/scm/lily-library.scm b/scm/lily-library.scm
index da04e6c89662e769015a0f8e2b0b3491d3378de6..cd9fb5b9a23ec4c75fd5430ceb8089a56aa5a737 100644
--- a/scm/lily-library.scm
+++ b/scm/lily-library.scm
@@ -411,17 +411,11 @@ bookoutput function"
 (define-public (first-member members lst)
   "Return first successful member (of member) from @var{members} in
 @var{lst}."
-  (if (null? members)
-      #f
-      (let ((m (member (car members) lst)))
-       (if m m (first-member (cdr members) lst)))))
+  (any (lambda (m) (member m lst)) members))

 (define-public (first-assoc keys lst)
   "Return first successful assoc of key from @var{keys} in @var{lst}."
-  (if (null? keys)
-      #f
-      (let ((k (assoc (car keys) lst)))
-       (if k k (first-assoc (cdr keys) lst)))))
+  (any (lambda (k) (assoc k lst)) keys))

 (define-public (flatten-alist alist)
   (if (null? alist)
@@ -477,30 +471,23 @@ For example:
 ;; hash

 (define-public (hash-table->alist t)
-  (hash-fold (lambda (k v acc) (acons  k v  acc))
-            '() t))
+  (hash-fold acons '() t))

 ;; todo: code dup with C++.
 (define-safe-public (alist->hash-table lst)
   "Convert alist to table"
   (let ((m (make-hash-table (length lst))))
-    (map (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
+    (for-each (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
     m))

 ;;;;;;;;;;;;;;;;
 ;; list

 (define (functional-or . rest)
-  (if (pair? rest)
-      (or (car rest)
-          (apply functional-or (cdr rest)))
-      #f))
+  (any identity rest))

 (define (functional-and . rest)
-  (if (pair? rest)
-      (and (car rest)
-          (apply functional-and (cdr rest)))
-      #t))
+  (every identity rest))

 (define (split-list lst n)
   "Split LST in N equal sized parts"
@@ -518,14 +505,7 @@ For example:
   (helper lst (make-vector n '()) (1- n)))

 (define (list-element-index lst x)
-  (define (helper todo k)
-    (cond
-     ((null? todo) #f)
-     ((equal? (car todo) x) k)
-     (else
-      (helper (cdr todo) (1+ k)))))
-
-  (helper lst 0))
+  (list-index (lambda (m) (equal? m x))))

 (define-public (count-list lst)
   "Given @var{lst} as @code{(E1 E2 .. )}, return



reply via email to

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