From 66c7a0744ccd17fda31be41517ff017671f8f660 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 11 Jul 2020 13:25:54 +0800 Subject: [PATCH] [eguile reports] eradicate pythonic for loops, use for-each instead this syntax is pythonic rather than lispy, is not recognized by code highlighters, and is not necessary to seasoned schemers. --- gnucash/report/eguile-utilities.scm | 15 +++- .../report/reports/standard/balsheet-eg.scm | 17 ++-- .../reports/support/balsheet-eg.eguile.scm | 82 +++++++++---------- 3 files changed, 59 insertions(+), 55 deletions(-) diff --git a/gnucash/report/eguile-utilities.scm b/gnucash/report/eguile-utilities.scm index 3005777a26..e44c97e26a 100644 --- a/gnucash/report/eguile-utilities.scm +++ b/gnucash/report/eguile-utilities.scm @@ -99,8 +99,11 @@ ;; If no file is found, returns just 'fname' for use in error messages. (find-internal "templates" fname)) -; 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). +;; 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). Note: deprecated in 4.x, removal in 5.x. this syntax +;; is pythonic rather than lispy, is not recognized by code +;; highlighters, and is not necessary to seasoned schemers. (export for) (define-syntax for (syntax-rules (for in do) @@ -110,8 +113,12 @@ ;; Note that this template must be defined before the ;; next one, since the template are evaluated in-order. ((for ( ...) in ( ...) do ...) - (for-each (lambda ( ...) ...) ...)) + (begin + (issue-deprecation-warning "for loops are deprecated. use for-each instead.") + (for-each (lambda ( ...) ...) ...))) ;; Single variable and list. e.g.: (for a in lst do (display a)) ((for in do ...) - (for-each (lambda () ...) )))) + (begin + (issue-deprecation-warning "for loops are deprecated. use for-each instead.") + (for-each (lambda () ...) ))))) diff --git a/gnucash/report/reports/standard/balsheet-eg.scm b/gnucash/report/reports/standard/balsheet-eg.scm index ee35e76f68..c591acfe06 100644 --- a/gnucash/report/reports/standard/balsheet-eg.scm +++ b/gnucash/report/reports/standard/balsheet-eg.scm @@ -46,12 +46,12 @@ (define debugging? #f) (define (debug . args) - (if debugging? - (for arg in args do - (if (string? arg) - (display (string-append arg " ")) - (display (string-append (dump arg) " ")))) - )) + (when debugging? + (for-each + (lambda (arg) + (display (if (string? arg) arg (dump arg))) + (display " ")) + args))) (define (hrule cols) ; in fact just puts in an empty row for spacing (display "") - (for sub-accrec in (accrec-sublist accrec) do + (for-each + (lambda (sub-accrec) (display "\n
  • ") (accrec-printer sub-accrec port) (display "
  • ")) + (accrec-sublist accrec)) (display "")) (display "#f"))) (define accrectype (make-record-type "accrecc" @@ -122,6 +124,7 @@ subtotal-cc ; of sublist plus this a/c sublist) accrec-printer)) + (define newaccrec-full (record-constructor accrectype)) ; requires all the fields (define (newaccrec-clean) ;; Create a new accrec with 'clean' empty values, e.g. strings are "", not #f diff --git a/gnucash/report/reports/support/balsheet-eg.eguile.scm b/gnucash/report/reports/support/balsheet-eg.eguile.scm index 17a62a56fc..3019629594 100644 --- a/gnucash/report/reports/support/balsheet-eg.eguile.scm +++ b/gnucash/report/reports/support/balsheet-eg.eguile.scm @@ -76,52 +76,46 @@ onedepth1) ;; Recursively display the accounts table from the given tree ;; (as returned by process-acc-list) - (for accrec in tree do - (let ((rshift2 0) ; adjust the amount column by this much - (showamt? #t)) ; whether to show the amount (e.g. not if zero) - (if (and (accrec-sublist accrec) - ; (> (accrec-depth accrec) 0)) - ) - ; has sub-accounts: shift left to put balance in same column as sub-accounts - (set! rshift2 -1)) - ; Don't show zero amount for a placeholder -- the value to - ; test for zero depends on whether or not this is a 'summary' value - ; (i.e. a total of sub-accounts that are not shown separately) - (if (and (accrec-placeholder? accrec) - (if (accrec-summary? accrec) - (not (accrec-non-zero? accrec)) - (gnc-numeric-zero-p (accrec-balance-num accrec)))) - (set! showamt? #f)) - (display-acc-row + (for-each + (lambda (accrec) + (display-acc-row + maxdepth + (accrec-depth accrec) + ;; has sub-accounts: shift left to put balance in same column + ;; as sub-accounts + (+ rshift (if (accrec-sublist accrec) -1 0)) + (accrec-namelink accrec) + ;; Don't show zero amount for a placeholder -- the value to + ;; test for zero depends on whether or not this is a 'summary' + ;; value (i.e. a total of sub-accounts that are not shown + ;; separately) + (cond + ((and (accrec-placeholder? accrec) + (if (accrec-summary? accrec) + (not (accrec-non-zero? accrec)) + (zero? (accrec-balance-num accrec)))) + " ") + ((accrec-summary? accrec) (format-comm-coll (accrec-subtotal-cc accrec))) + (else (format-monetary (accrec-balance-mny accrec)))) + (< (accrec-depth accrec) 1); total? + #f) ; leftoverrule? + (when (accrec-sublist accrec) + ;; recurse to deeper accounts... + (display-accounts-table-r + (accrec-sublist accrec) neg? maxdepth rshift onedepth1) + ;; ...and then display the total + ;; unless there is only one depth-1 account + (unless (and onedepth1 (= 1 (accrec-depth accrec))) + (display-acc-row maxdepth (accrec-depth accrec) - (+ rshift rshift2) - (accrec-namelink accrec) - (if showamt? - (if (accrec-summary? accrec) - (format-comm-coll (accrec-subtotal-cc accrec)) - (format-monetary (accrec-balance-mny accrec))) - " " - ) - (< (accrec-depth accrec) 1); total? - #f) ; leftoverrule? - (if (accrec-sublist accrec) - (begin - ; recurse to deeper accounts... - (display-accounts-table-r (accrec-sublist accrec) neg? maxdepth rshift onedepth1) - ; ...and then display the total - ; unless there is only one depth-1 account - (if (not (and onedepth1 - (= 1 (accrec-depth accrec)))) - (display-acc-row - maxdepth - (accrec-depth accrec) - (if (> (accrec-depth accrec) 1) rshift 0) - (string-append (_ "Total") " " (accrec-namelink accrec)) - (format-comm-coll-total (accrec-subtotal-cc accrec)) - (<= (accrec-depth accrec) 1) ; total? - (> (accrec-depth accrec) 0))))))) ; leftoverrule? - ) + (if (> (accrec-depth accrec) 1) rshift 0) + (string-append (_ "Total") " " (accrec-namelink accrec)) + (format-comm-coll-total (accrec-subtotal-cc accrec)) + (<= (accrec-depth accrec) 1) ; total? + (> (accrec-depth accrec) 0))))) ; leftoverrule? + + tree)) ?>