mirror of
https://github.com/Gnucash/gnucash.git
synced 2026-09-03 20:53:02 -05:00
Bug #635632: Add custom for loop syntax for eguile templates
Patch by Matthijs Kooijman <matthijs@stdin.nl>: The built-in for-each loop construct is a bit cumbersome: It always requires an explicit lambda and the list to loop over is the last argument. Especially the latter makes it very hard to read when the lambda is big and multiple for-each'es are nested. For hashes, this prevents the need of the cumbersome hash-fold and slightly better hash-for-each (which still suffers from the same problems as for-each and is not available in guile 1.6). This new syntax allows for three distinct syntaxes: * Looping over a single list: (for a in lst do (display a)) * Looping over multiple lists: (for (a b) in (lsta lstb) do (display (+ a b))) * Looping over a hash: (for key => value in hash do (display (* key value))) git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@19888 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
(gnc:module-load "gnucash/business-utils" 0)
|
||||
(use-modules (gnucash report standard-reports))
|
||||
(use-modules (gnucash report business-reports))
|
||||
(use-modules (ice-9 syncase)) ; for define-syntax
|
||||
|
||||
|
||||
;(use-modules (srfi srfi-13)) ; for extra string functions
|
||||
@@ -88,4 +89,32 @@
|
||||
(if (access? syspath R_OK)
|
||||
syspath
|
||||
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
|
||||
(syntax-rules (for in => do hash)
|
||||
; Multiple variables and 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)
|
||||
|
||||
Reference in New Issue
Block a user