[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.
This commit is contained in:
Christopher Lam 2020-07-11 13:25:54 +08:00
parent 17727a7b77
commit 66c7a0744c
3 changed files with 59 additions and 55 deletions

View File

@ -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 (<var> ...) in (<list> ...) do <expr> ...)
(for-each (lambda (<var> ...) <expr> ...) <list> ...))
(begin
(issue-deprecation-warning "for loops are deprecated. use for-each instead.")
(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>))))
(begin
(issue-deprecation-warning "for loops are deprecated. use for-each instead.")
(for-each (lambda (<var>) <expr> ...) <list>)))))

View File

@ -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 "<tr valign=\"center\"><td colspan=\"")
@ -102,10 +102,12 @@
(display " sublist: ") (if (accrec-sublist accrec)
(begin
(display "\n<ul>")
(for sub-accrec in (accrec-sublist accrec) do
(for-each
(lambda (sub-accrec)
(display "\n<li>")
(accrec-printer sub-accrec port)
(display "</li>"))
(accrec-sublist accrec))
(display "</ul>"))
(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

View File

@ -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))))
"&nbsp;")
((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)))
"&nbsp;"
)
(< (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))
?>
<!-- The HTML starts here... -->