mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 07:33:01 +00:00 
			
		
		
		
	 86d2785d5e
			
		
	
	86d2785d5e
	
	
	
		
			
			that may replace or complement the loop macro. The loop macro is still useful though and not nearly as complicated as the common lisp loop macro.
		
			
				
	
	
		
			74 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Janet
		
	
	
	
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Janet
		
	
	
	
	
	
| # Simpler iteration primitives example.
 | |
| 
 | |
| (defn- iter-for
 | |
|   [prelude binding start end body]
 | |
|   (def $end (gensym))
 | |
|   (tuple 'do
 | |
|          prelude
 | |
|          (tuple 'var binding start)
 | |
|          (tuple 'def $end end)
 | |
|          (tuple 'while (tuple < binding $end)
 | |
|                 body
 | |
|                 (tuple '++ binding))))
 | |
| 
 | |
| (defn- iter-keys
 | |
|   [prelude binding tab body]
 | |
|   (tuple 'do
 | |
|          prelude
 | |
|          (tuple 'var binding (tuple next tab nil))
 | |
|          (tuple 'while (tuple not= nil binding)
 | |
|                 body
 | |
|                 (tuple := binding (tuple next tab binding)))))
 | |
| 
 | |
| (defmacro do-range
 | |
|   "Iterate over a half open integer range."
 | |
|   [binding start end & body]
 | |
|   (def $iter (gensym))
 | |
|   (iter-for nil $iter start end
 | |
|             (apply tuple 'do (tuple 'def binding $iter) body)))
 | |
| 
 | |
| (defmacro each
 | |
|   "Iterate over an indexed data structure."
 | |
|   [binding ind & body]
 | |
|   (def $iter (gensym))
 | |
|   (def $ind (gensym))
 | |
|   (iter-for (tuple 'def $ind ind)
 | |
|             $iter 0 (tuple length $ind)
 | |
|             (apply tuple 'do (tuple 'def binding (tuple get $ind $iter)) body)))
 | |
| 
 | |
| 
 | |
| (defmacro each-key
 | |
|   "Iterate over keys of a table or structure."
 | |
|   [binding tab & body]
 | |
|   (def $tab (gensym))
 | |
|   (def $key (gensym))
 | |
|   (iter-keys
 | |
|              (tuple 'def $tab tab)
 | |
|              $key
 | |
|              $tab
 | |
|              (apply tuple 'do (tuple 'def binding $key) body)))
 | |
| 
 | |
| (defmacro each-value
 | |
|   "Iterate over values of a table or structure."
 | |
|   [binding tab & body]
 | |
|   (def $tab (gensym))
 | |
|   (def $key (gensym))
 | |
|   (iter-keys
 | |
|              (tuple 'def $tab tab)
 | |
|              $key
 | |
|              $tab
 | |
|              (apply tuple 'do (tuple 'def binding (tuple 'get $tab $key)) body)))
 | |
| 
 | |
| (defmacro each-pair
 | |
|   "Iterate over keys and values of a table or structure."
 | |
|   [k v tab & body]
 | |
|   (def $tab (gensym))
 | |
|   (def $key (gensym))
 | |
|   (iter-keys
 | |
|              (tuple 'def $tab tab)
 | |
|              $key
 | |
|              $tab
 | |
|              (apply tuple 'do 
 | |
|                     (tuple 'def k $key)
 | |
|                     (tuple 'def v (tuple 'get $tab $key)) body)))
 |