[eguile-utilities] compact functions

* whitespace
* move string-repeat to eguile-html-utilities where it's used
This commit is contained in:
Christopher Lam 2019-07-28 13:36:22 +08:00
parent e3a695d0d4
commit e506b7c332

View File

@ -33,7 +33,6 @@
(gnc:module-load "gnucash/report/report-system" 0) (gnc:module-load "gnucash/report/report-system" 0)
(gnc:module-load "gnucash/app-utils" 0) (gnc:module-load "gnucash/app-utils" 0)
(define-public (fmtnumber n) (define-public (fmtnumber n)
;; Format a number (integer or real) into something printable ;; Format a number (integer or real) into something printable
(number->string (if (integer? n) (number->string (if (integer? n)
@ -46,28 +45,18 @@
(define-public (gnc-monetary-neg? monetary) (define-public (gnc-monetary-neg? monetary)
; return true if the monetary value is negative ; return true if the monetary value is negative
(gnc-numeric-negative-p (gnc:gnc-monetary-amount monetary))) (negative? (gnc:gnc-monetary-amount monetary)))
(define-public (string-repeat s n)
;; return a string made of n copies of string s
;; (there's probably a better way)
(let ((s2 ""))
(do ((i 1 (1+ i))) ((> i n))
(set! s2 (string-append s2 s)))
s2))
;; 'Safe' versions of cdr and cadr that don't crash ;; 'Safe' versions of cdr and cadr that don't crash
;; if the list is empty (is there a better way?) ;; if the list is empty (is there a better way?)
(define-public (safe-cdr l) (define-public (safe-cdr l)
(if (null? l) (if (null? l) '()
'() (cdr l)))
(cdr l)))
(define-public (safe-cadr l) (define-public (safe-cadr l)
(if (null? l) (cond
'() ((null? l) '())
(if (null? (cdr l)) ((null? (cdr l)) '())
'() (else (cadr l))))
(cadr l))))
(define-public (find-file fname) (define-public (find-file fname)
;; Find the file 'fname', and return its full path. ;; Find the file 'fname', and return its full path.
@ -75,40 +64,27 @@
;; Then look in Gnucash's standard report directory. ;; Then look in Gnucash's standard report directory.
;; If no file is found, returns just 'fname' for use in error messages. ;; If no file is found, returns just 'fname' for use in error messages.
;; Note: this has been tested on Linux and Windows Vista so far... ;; Note: this has been tested on Linux and Windows Vista so far...
(let* ((userpath (gnc-build-userdata-path fname)) (let ((userpath (gnc-build-userdata-path fname))
(syspath (gnc-build-report-path fname))) (syspath (gnc-build-report-path fname)))
; make sure there's a trailing delimiter ;; make sure there's a trailing delimiter
(if (access? userpath R_OK) (cond
userpath ((access? userpath R_OK) userpath)
(if (access? syspath R_OK) ((access? syspath R_OK) syspath)
syspath (else fname))))
fname))))
; Define syntax for more readable for loops (the built-in for-each requires an ; Define syntax for more readable for loops (the built-in for-each requires an
; explicit lambda and has the list expression all the way at the end). ; explicit lambda and has the list expression all the way at the end).
(define-syntax for
(syntax-rules (for in => do hash)
; Multiple variables and equal number of lists (in
; parenthesis). e.g.:
;
; (for (a b) in (lsta lstb) do (display (+ a b)))
;
; Note that this template must be defined before the
; next one, since the template are evaluated in-order.
((for (<var> ...) in (<list> ...) do <expr> ...)
(for-each (lambda (<var> ...) <expr> ...) <list> ...))
; Single variable and list. e.g.:
;
; (for a in lst do (display a))
((for <var> in <list> do <expr> ...)
(for-each (lambda (<var>) <expr> ...) <list>))
; Iterate over key & values in a hash. e.g.:
;
; (for key => value in hash do (display (* key value)))
((for <key> => <value> in <hash> do <expr> ...)
; We use fold to iterate over the hash (instead of
; hash-for-each, since that is not present in guile
; 1.6).
(hash-fold (lambda (<key> <value> accum) (begin <expr> ... accum)) *unspecified* <hash>))
))
(export for) (export for)
(define-syntax for
(syntax-rules (for in do)
;; Multiple variables and equal number of lists (in
;; parenthesis). e.g.:
;; (for (a b) in (lsta lstb) do (display (+ a b)))
;; Note that this template must be defined before the
;; next one, since the template are evaluated in-order.
((for (<var> ...) in (<list> ...) do <expr> ...)
(for-each (lambda (<var> ...) <expr> ...) <list> ...))
;; Single variable and list. e.g.: (for a in lst do (display a))
((for <var> in <list> do <expr> ...)
(for-each (lambda (<var>) <expr> ...) <list>))))