shithub: mc

ref: dbe60482e84a28a55ae6d9f5a7c9728e00f690d3
dir: /lib/iter/chunk.myr/

View raw version
use std

pkg iter =
	type chunkiter(@a) = struct
		idx	: std.size
		blksz	: std.size
		sl	: @a[:]
	;;

	impl iterable chunkiter(@a) -> @a[:]
	generic bychunk	: (a : @a[:], chunk : std.size -> chunkiter(@a))
;;

generic bychunk = {a, sz
	-> [.sl = a, .idx = 0, .blksz = std.max(sz, 1)]
}

impl iterable chunkiter(@a) -> @a[:] =
	__iternext__ = {itp, valp
		var len

		len = std.min(itp.blksz, itp.sl.len - itp.idx)
		if itp.idx + len > itp.sl.len || itp.idx >= itp.sl.len
			-> false
		;;
		valp# = itp.sl[itp.idx: itp.idx + len]
		itp.idx += itp.blksz
		-> true
	}

	__iterfin__ = {itp, valp
	}
;;