r19888 - gnucash/trunk/src/report/report-system - Bug #635632: Add custom for loop syntax for eguile templates

Christian Stimming cstim at code.gnucash.org
Sat Nov 27 16:54:19 EST 2010


Author: cstim
Date: 2010-11-27 16:54:18 -0500 (Sat, 27 Nov 2010)
New Revision: 19888
Trac: http://svn.gnucash.org/trac/changeset/19888

Modified:
   gnucash/trunk/src/report/report-system/eguile-utilities.scm
Log:
Bug #635632: Add custom for loop syntax for eguile templates

Patch by Matthijs Kooijman <matthijs at 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)))

Modified: gnucash/trunk/src/report/report-system/eguile-utilities.scm
===================================================================
--- gnucash/trunk/src/report/report-system/eguile-utilities.scm	2010-11-27 21:01:15 UTC (rev 19887)
+++ gnucash/trunk/src/report/report-system/eguile-utilities.scm	2010-11-27 21:54:18 UTC (rev 19888)
@@ -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)



More information about the gnucash-changes mailing list