enso.nim: Added file operations

This commit is contained in:
buckwheat
2025-12-26 22:18:24 -08:00
parent 996c115a1c
commit e263a216d6
2 changed files with 53 additions and 15 deletions

View File

@ -1,5 +1,5 @@
import enso
import std/[os, strformat, sugar]
import std/[options, strformat, sugar]
proc unsafe_void(str: string) =
echo str
@ -8,13 +8,6 @@ proc unsafe_int(str: string): int =
echo str
result = 1
func check_file(name: string): IO[string] =
proc(): string =
if fileExists(name):
result = readFile(name)
else:
result = "Not found"
func main() =
run strput("Hello from Enso!")
@ -32,13 +25,29 @@ func main() =
name()
let
nested: IO[IO[string]] = get_filename.map(check_file)
joined: IO[string] = nested.join
nested: IO[IO[Option[string]]] = get_filename.map(fileread)
joined: IO[Option[string]] = nested.join
join_opt: Option[string] = joined()
run strput(joined())
if join_opt.isSome:
run strput(join_opt.get)
else:
run strput("joined had no data")
let flat: IO[string] = get_filename.flatmap(check_file)
run strput(flat())
let
flat: IO[Option[string]] = get_filename.flatmap(fileread)
flat_opt: Option[string] = flat()
if flat_opt.isSome:
run strput(flat_opt.get)
else:
run strput("flat had no data")
let writer: Error = run filewrite("Hello world!\n", "test.txt")
if writer == OK:
run strput("Successfully wrote test.txt!")
else:
run strput("Could not write test.txt!")
when isMainModule:
main()