From b520122d90925a5c4d56c17c49f51521cc6a3b87 Mon Sep 17 00:00:00 2001 From: Tobias Mock Date: Tue, 7 Nov 2023 22:08:02 +0100 Subject: [PATCH] Add `with-open` to in_channel module This now works since we have unit values --- std/in_channel.smu | 6 ++++++ 1 file changed, 6 insertions(+) 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))))))