mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 15:43:01 +00:00 
			
		
		
		
	Add file/lines iterator
This commit is contained in:
		| @@ -1749,6 +1749,19 @@ | ||||
|   (printf (dyn *pretty-format* "%q") x) | ||||
|   (flush)) | ||||
|  | ||||
| (defn file/lines | ||||
|   "Return an iterator over the lines of a file" | ||||
|   [file-or-path &opt mode] | ||||
|   (default mode :r) | ||||
|   (if (bytes? file-or-path) | ||||
|     (coro | ||||
|       (with [f (file/open file-or-path mode)] | ||||
|         (while (def line (file/read f :line)) | ||||
|           (yield line)))) | ||||
|     (coro | ||||
|       (while (def line (file/read file-or-path :line)) | ||||
|         (yield line))))) | ||||
|  | ||||
| ### | ||||
| ### | ||||
| ### Pattern Matching | ||||
|   | ||||
| @@ -102,6 +102,19 @@ | ||||
|     (os/execute [janet "-e" `(repeat 20 (print :hello))`] :p {:out f}) | ||||
|     (file/flush f))) | ||||
|  | ||||
| # each-line iterator | ||||
|  | ||||
| (assert-no-error "file/lines iterator" | ||||
|    (def outstream (os/open "unique.txt" :wct)) | ||||
|    (def buf1 "123\n456\n") | ||||
|    (defer (:close outstream) | ||||
|      (:write outstream buf1)) | ||||
|    (var buf2 "") | ||||
|    (each line (file/lines "unique.txt") | ||||
|       (set buf2 (string buf2 line))) | ||||
|    (assert (= buf1 buf2) "file/lines iterator") | ||||
|    (os/rm "unique.txt")) | ||||
|  | ||||
| # Issue #593 | ||||
| (assert-no-error "file writing 3" | ||||
|   (def outfile (file/open "unique.txt" :w)) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Ico Doornekamp
					Ico Doornekamp