Skip to content

Commit

Permalink
Add with-open to in_channel module
Browse files Browse the repository at this point in the history
This now works since we have unit values
  • Loading branch information
tjammer committed Nov 7, 2023
1 parent b212909 commit b520122
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions std/in_channel.smu
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(def close (fun t unit))

-- high level interface
(def with-open (fun string/t (fun t& 'a) (option/t 'a)))
(def readall (fun t& string/t))
(def lines (fun t& (fun string/t unit) unit))

Expand Down Expand Up @@ -37,6 +38,11 @@

(defn close [ic] (ignore (fclose ic.file)))

(defn with-open [name f]
(match (open name)
((#some ic) (let [ic& !ic ret (f &ic)] (close ic) (#some ret)))
(#none #none)))

(defn fill [buf& icbuf lower upper]
(prelude/iter-range lower upper
(fn [i] (array/push &buf !(copy icbuf.(i))))))
Expand Down

0 comments on commit b520122

Please sign in to comment.