Skip to content

Commit

Permalink
Merge pull request #1157 from zevv/file-lines
Browse files Browse the repository at this point in the history
Add file/lines iterator
  • Loading branch information
bakpakin committed May 26, 2023
2 parents 77e62a2 + 09345ec commit 70f13f1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/boot/boot.janet
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,14 @@
(printf (dyn *pretty-format* "%q") x)
(flush))


(defn file/lines
"Return an iterator over the lines of a file."
[file]
(coro
(while (def line (file/read file :line))
(yield line))))

###
###
### Pattern Matching
Expand Down
14 changes: 14 additions & 0 deletions test/suite0009.janet
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@
(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 "")
(with [f (file/open "unique.txt" :r)]
(each line (file/lines f)
(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))
Expand Down

0 comments on commit 70f13f1

Please sign in to comment.