shithub: mc

ref: fdf87b91c1bbd64be75b8438ad6eaf492325e5b4
dir: /lib/bio/iter.myr/

View raw version
use std
use "bio"
use "types"

pkg bio =
	type lineiter = file#
	type chariter = file#
	impl iterable lineiter -> byte[:]
	impl iterable chariter -> char

	const byline	: (f : file# -> lineiter)
	const bychar	: (f : file# -> chariter)
;;

const byline = {f
	-> (f : lineiter)
}

impl iterable lineiter -> byte[:] =
	__iternext__ = {itp, outp
		match bio.readln((itp# : file#))
		| `std.Ok ln:	outp# = ln
		| `std.Err _:	-> false
		;;
		-> true
	}

	__iterfin__ = {itp, outp
		std.slfree(outp#)
	}
;;

const bychar = {f
	-> (f : chariter)
}

impl iterable chariter -> char =
	__iternext__ = {itp, outp : char#
		match bio.getc((itp# : file#))
		| `std.Ok c:	outp# = c
		| `std.Err _:	-> false
		;;
		-> true
	}

	__iterfin__ = {itp, outp
	}
;;