shithub: mc

ref: 16ed56af9183b9dc0669adf1b8b5eade47902d38
dir: /lib/iter/reverse.myr/

View raw version
use std

pkg iter =
	type reverseiter(@a) = struct
		sl	: @a[:]
		idx	: std.size
	;;
	impl iterable reverseiter(@a) -> @a
	generic byreverse 	: (sl : @a[:] -> reverseiter(@a))
;;

generic byreverse = {sl
	-> [.sl = sl, .idx = sl.len]
}

impl iterable reverseiter(@a) -> @a =
	__iternext__ = {itp, valp
		if itp.idx > 0
			valp# = itp.sl[itp.idx - 1]
			itp.idx--
			-> true
		else
			-> false
		;;
	}

	__iterfin__ = {itp, valp
	}
;;