diff --git a/std/in_channel.smu b/std/in_channel.smu index 97d5afcf..8556f1ca 100644 --- a/std/in_channel.smu +++ b/std/in_channel.smu @@ -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)) @@ -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))))))