Added hashing, hashqing hashving och in-hash.
This commit is contained in:
		
							parent
							
								
									5f96ef4fb0
								
							
						
					
					
						commit
						f492a5278b
					
				
					 3 changed files with 82 additions and 26 deletions
				
			
		|  | @ -197,10 +197,10 @@ | ||||||
| 
 | 
 | ||||||
|     <subsection title=":for-clauses"> |     <subsection title=":for-clauses"> | ||||||
|       <dl> |       <dl> | ||||||
|         <dt type="syntax">(:for identifier (in start [update [stop]]))</dt> |         <dt type="syntax">(:for binding (in start [update [stop]]))</dt> | ||||||
|         <dd> |         <dd> | ||||||
|           <p> |           <p> | ||||||
|             Binds a loop variable to <code>identifier</code>. It's first value is <code>start</code>. It is updated by the <code>update</code> expression, or is left unchanged if no such expression is present. If a <code>stop</code> expression is provided, it will be evaluated before each loop body. If the <code>stop</code> expression returns true, the iteration will be considered exhausted. |             Binds a loop variable to <code>binding</code>. It's first value is <code>start</code>. It is updated by the <code>update</code> expression, or is left unchanged if no such expression is present. If a <code>stop</code> expression is provided, it will be evaluated before each loop body. If the <code>stop</code> expression returns true, the iteration will be considered exhausted. | ||||||
| 
 | 
 | ||||||
|             <example> |             <example> | ||||||
|               (loop ((:for a (in 0 b)) (:for b (in 1 (+ a b) (> b 20)))) |               (loop ((:for a (in 0 b)) (:for b (in 1 (+ a b) (> b 20)))) | ||||||
|  | @ -210,40 +210,50 @@ | ||||||
|           </p> |           </p> | ||||||
|         </dd> |         </dd> | ||||||
| 
 | 
 | ||||||
|         <dt type="syntax">(:for identifier (up-from start [(to bound)] [(by step)])</dt> |         <dt type="syntax">(:for binding (up-from start [(to bound)] [(by step)])</dt> | ||||||
|         <dt type="syntax">(:for identifier (up-from start [bound [by]]))</dt> |         <dt type="syntax">(:for binding (up-from start [bound [by]]))</dt> | ||||||
|         <dd>Binds <code>identifier</code> to the number <code>start</code> up to <code>bound</code> (exclusive!) by <code>step</code>. If no <code>bound</code> is given, it will yield values indefinitely. The second shorter form will not allow unbounded iteratiom.</dd> |         <dd>Binds <code>binding</code> to the number <code>start</code> up to <code>bound</code> (exclusive!) by <code>step</code>. If no <code>bound</code> is given, it will yield values indefinitely. The second shorter form will not allow unbounded iteratiom.</dd> | ||||||
| 
 | 
 | ||||||
|         <dt type="syntax">(:for identifier (down-from start [(to bound)] [(by step)])</dt> |         <dt type="syntax">(:for binding (down-from start [(to bound)] [(by step)])</dt> | ||||||
|         <dt type="syntax">(:for identifier (down-from start [bound [by]]))</dt> |         <dt type="syntax">(:for binding (down-from start [bound [by]]))</dt> | ||||||
|         <dd>Binds <code>identifier</code> to the number <code>start</code> down to <code>bound</code> (inclusive!) by <code>step</code>. If no <code>bound</code> is given, it will yield values indefinitely. The second shorter form will not allow unbounded iteratiom.</dd> |         <dd>Binds <code>binding</code> to the number <code>(- start 1)</code> down to <code>bound</code> (inclusive!) by <code>step</code>. If no <code>bound</code> is given, it will yield values indefinitely. The second shorter form will not allow unbounded iteratiom.</dd> | ||||||
| 
 | 
 | ||||||
|         <dt type="syntax">(:for identifier [pair] (in-list expr [by])</dt> |         <dt type="syntax">(:for binding [pair] (in-list expr [by])</dt> | ||||||
|         <dd>Binds <code>identifier</code> to the car of the loop variable <code>pair</code>. <code>pair</code> is advanced by applying the procedure <code>by</code> to it (defaulting to <code>cdr</code>). The iteration stops when <code>pair</code> is the empty list.</dd> |         <dd>Binds <code>binding</code> to the car of the loop variable <code>pair</code>. <code>pair</code> is advanced by applying the procedure <code>by</code> to it (defaulting to <code>cdr</code>). The iteration stops when <code>pair</code> is the empty list.</dd> | ||||||
| 
 | 
 | ||||||
|         <dd type="syntax">(:for identifier [pairs] (in-lists expr [by])</dd> |         <dd type="syntax">(:for binding [pairs] (in-lists expr [by])</dd> | ||||||
|         <dt>Works the same as <code>in-list</code>, but <code>expr</code> must evaluate to a list of lists. <code>identifier</code> is bound to the car of those lists, and they are advanced by <code>by</code>, defaulting to <code>cdr</code>.</dt> |         <dt>Works the same as <code>in-list</code>, but <code>expr</code> must evaluate to a list of lists. <code>binding</code> is bound to the car of those lists, and they are advanced by <code>by</code>, defaulting to <code>cdr</code>.</dt> | ||||||
| 
 | 
 | ||||||
|         <dd type="syntax">(:for identifier [index] (in-vector expr [low [high]]))</dd> |         <dd type="syntax">(:for binding [index] (in-vector expr [low [high]]))</dd> | ||||||
|         <dt>Binds <code>identifier</code> to all elements in the vector produced by <code>expr</code> in order from <code>low</code> to <code>high</code>. <code>low</code> defaults to 0 and <code>high</code> defaults to the last index of the vector.</dt> |         <dt>Binds <code>binding</code> to all elements in the vector produced by <code>expr</code> in order from <code>low</code> to <code>high</code>. <code>low</code> defaults to 0 and <code>high</code> defaults to the last index of the vector.</dt> | ||||||
| 
 | 
 | ||||||
|         <dd type="syntax">(:for identifier [index] (in-reverse-vector expr [high [low]]))</dd> |         <dd type="syntax">(:for binding [index] (in-reverse-vector expr [high [low]]))</dd> | ||||||
|         <dt>Binds <code>identifier</code> to all elements in the vector produced by <code>expr</code> in reverse order from <code>high</code> to <code>low</code>. <code>high</code> defaults to the last element of the vector and <code>low</code> defaults to 0.</dt> |         <dt>Binds <code>binding</code> to all elements in the vector produced by <code>expr</code> in reverse order from <code>high</code> to <code>low</code>. <code>high</code> defaults to the last element of the vector and <code>low</code> defaults to 0.</dt> | ||||||
| 
 | 
 | ||||||
|         <dd type="syntax">(:for identifier [index] (in-string expr [low [high]]))</dd> |         <dd type="syntax">(:for binding [index] (in-string expr [low [high]]))</dd> | ||||||
|         <dt>Binds <code>identifier</code> to all elements in the string produced by <code>expr</code> in order from <code>low</code> to <code>high</code>. <code>low</code> defaults to 0 and <code>high</code> defaults to the last index of the string.</dt> |         <dt>Binds <code>binding</code> to all elements in the string produced by <code>expr</code> in order from <code>low</code> to <code>high</code>. <code>low</code> defaults to 0 and <code>high</code> defaults to the last index of the string.</dt> | ||||||
| 
 | 
 | ||||||
|         <dd type="syntax">(:for identifier [index] (in-reverse-string expr [high [low]]))</dd> |         <dd type="syntax">(:for binding [index] (in-reverse-string expr [high [low]]))</dd> | ||||||
|         <dt>Binds <code>identifier</code> to all elements in the vector produced by <code>expr</code> in reverse order from <code>high</code> to <code>low</code>. <code>high</code> defaults to the last element of the vector and <code>low</code> defaults to 0.</dt> |         <dt>Binds <code>binding</code> to all elements in the vector produced by <code>expr</code> in reverse order from <code>high</code> to <code>low</code>. <code>high</code> defaults to the last element of the vector and <code>low</code> defaults to 0.</dt> | ||||||
| 
 | 
 | ||||||
|         <dd type="syntax">(:for identifier (in-port port [reader [eof?]]))</dd> |         <dd type="syntax">(:for binding (in-port port [reader [eof?]]))</dd> | ||||||
|         <dt>Binds <code>identifier</code> to the result of calling <code>reader</code> on <code>port</code>. Iteration stops when <code>(eof? identifier)</code> returns true.</dt> |         <dt>Binds <code>binding</code> to the result of calling <code>reader</code> on <code>port</code>. Iteration stops when <code>(eof? binding)</code> returns true.</dt> | ||||||
| 
 | 
 | ||||||
|         <dd type="syntax">(:for identifier (in-file path [reader [eof?]]))</dd> |         <dd type="syntax">(:for binding (in-file path [reader [eof?]]))</dd> | ||||||
|         <dt>Opens the file located at <code>path</code> (which is a string) and binds <code>identifier</code> to the result of calling <code>reader</code> on the opened port. Iteration stops when <code>(eof? identifier)</code> returns true.</dt> |         <dt>Opens the file located at <code>path</code> (which is a string) and binds <code>binding</code> to the result of calling <code>reader</code> on the opened port. Iteration stops when <code>(eof? binding)</code> returns true.</dt> | ||||||
|  | 
 | ||||||
|  |         <dd type="syntax">(:for binding (in-generator gen))</dd> | ||||||
|  |         <dt>Binds binding to the result of calling the SRFI-158-compatible generator <code>gen</code>. Iteration stops when <code>gen</code> returns the end-of-file object.</dt> | ||||||
|  | 
 | ||||||
|  |         <dd type="syntax">(:for binding (in-hash hash))</dd> | ||||||
|  |         <dt>Binds <code>binding</code> to the <code>(key . value)</code> pairs of the hash-table <code>hash</code>. May, as all body-binding variables, be pattern-matched: | ||||||
|  | 
 | ||||||
|  |         <example> | ||||||
|  |           (loop/list (((_ . val) (in-hash hash-table))) | ||||||
|  |             val) | ||||||
|  |         </example> | ||||||
|  |         </dt> | ||||||
| 
 | 
 | ||||||
|         <dd type="syntax">(:for identifier (in-generator gen))</dd> |  | ||||||
|         <dt>Binds identifier to the result of calling the SRFI-158-compatible generator <code>gen</code>. Iteration stops when <code>gen</code> returns the end-of-file object.</dt> |  | ||||||
|          |          | ||||||
|       </dl> |       </dl> | ||||||
|              |              | ||||||
|  |  | ||||||
							
								
								
									
										2
									
								
								goof.scm
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								goof.scm
									
										
									
									
									
								
							|  | @ -58,6 +58,7 @@ | ||||||
| 
 | 
 | ||||||
|             in-vector in-reverse-vector |             in-vector in-reverse-vector | ||||||
|             in-string in-reverse-string |             in-string in-reverse-string | ||||||
|  |             in-hash | ||||||
| 
 | 
 | ||||||
|             in-port |             in-port | ||||||
|             in-file |             in-file | ||||||
|  | @ -72,6 +73,7 @@ | ||||||
|             appending-reverse |             appending-reverse | ||||||
|             summing |             summing | ||||||
|             multiplying |             multiplying | ||||||
|  |             hashing | ||||||
| 
 | 
 | ||||||
|             in-cycle |             in-cycle | ||||||
|             in-indexed |             in-indexed | ||||||
|  |  | ||||||
|  | @ -248,6 +248,20 @@ | ||||||
|     ((down-from ((var) (start limit)) next . rest) |     ((down-from ((var) (start limit)) next . rest) | ||||||
|      (down-from ((var) (start limit 1)) next . rest)))) |      (down-from ((var) (start limit 1)) next . rest)))) | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | (define-syntax in-hash | ||||||
|  |   (syntax-rules () | ||||||
|  |     ((_ ((bindings) (expr)) n . rest) | ||||||
|  |      (n | ||||||
|  |       () | ||||||
|  |       () | ||||||
|  |       ((cursor (hash-map->list cons expr) (cdr cursor))) | ||||||
|  |       ((not (pair? cursor))) | ||||||
|  |       ((bindings (car cursor))) | ||||||
|  |       () | ||||||
|  |       . rest)))) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| (define-syntax accumulating | (define-syntax accumulating | ||||||
|   (syntax-rules (initial if :acc) |   (syntax-rules (initial if :acc) | ||||||
|     ((accumulating :acc (kons final init) ((var) . x) next . rest) |     ((accumulating :acc (kons final init) ((var) . x) next . rest) | ||||||
|  | @ -317,6 +331,36 @@ | ||||||
|     ((multiplying :acc args next . rest) |     ((multiplying :acc args next . rest) | ||||||
|      (accumulating :acc (* (lambda (x) x) 1) args next . rest)))) |      (accumulating :acc (* (lambda (x) x) 1) args next . rest)))) | ||||||
| 
 | 
 | ||||||
|  | (define-syntax define-hashing | ||||||
|  |   (syntax-rules () | ||||||
|  |     ((_ name default-make setter) | ||||||
|  |      (define-syntax name | ||||||
|  |        (syntax-rules (:acc if initial) | ||||||
|  |          ((_ :acc ((var) (key value)) n . rest) | ||||||
|  |           (name :acc ((var) (key value (if #t) (initial defualt-make))) n . rest)) | ||||||
|  |          ;; either init or if | ||||||
|  |          ((_ :acc ((var) (key value (if guard))) n . rest) | ||||||
|  |           (name :acc ((var) (key value (if guard) (initial default-make))) n . rest)) | ||||||
|  |          ((_ :acc ((var) (key value (initial init))) n . rest) | ||||||
|  |           (name :acc ((var) (key value (if #t) (initial init))) n . rest)) | ||||||
|  |          ;; both init and if | ||||||
|  |          ((_ :acc ((var) (key value (initial init) (if guard))) n . rest) | ||||||
|  |           (name ((var) (key value (if guard) (initial init))) n . rest)) | ||||||
|  |          ((_ :acc ((var) (key value (if guard) (initial init))) n . rest) | ||||||
|  |           (n | ||||||
|  |            ((hash init)) | ||||||
|  |            ((dummy (if #f #f) (if guard (setter hash key value) (if #f #f)))) | ||||||
|  |            () | ||||||
|  |            () | ||||||
|  |            () | ||||||
|  |            ((var hash)) | ||||||
|  |            . rest))))))) | ||||||
|  | 
 | ||||||
|  | (define-hashing hashing (make-hash-table) hash-set!) | ||||||
|  | (define-hashing hashving (make-hash-table) hashv-set!) | ||||||
|  | (define-hashing hashqing (make-hash-table) hashq-set!) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| ;;; Here starts generator clauses. | ;;; Here starts generator clauses. | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Linus
						Linus