shithub: mc

Download patch

ref: a277cea8fabeb3ec38484ffc461ae86e5fd8f9b5
parent: 2e3e676b32b16cf0f73b508e7633a15b4184b9db
author: Ori Bernstein <ori@eigenstate.org>
date: Tue Nov 8 12:38:58 EST 2016

Add byref() iter utility.

	If you want to mutate the members of the slice, or if you
	just don't want to do a big memory copy, use this.

--- a/lib/std/iterutil.myr
+++ b/lib/std/iterutil.myr
@@ -14,9 +14,14 @@
 
 	type enumiter(@a) = struct
 		idx	: size
-		s	: @a[:]
+		sl	: @a[:]
 	;;
 
+	type refiter(@a) = struct
+		idx	: size
+		sl	: @a[:]
+	;;
+
 	impl iterable zipiter(@a, @b) -> (@a, @b)
 	impl iterable enumiter(@a) -> (size, @a)
 	impl iterable reverseiter(@a) -> @a
@@ -23,6 +28,7 @@
 
 	generic byzip	: (a : @a[:], b : @b[:]	 -> zipiter(@a, @b))
 	generic byenum	: (a : @a[:] -> enumiter(@a))
+	generic byref 	: (sl : @a[:] -> reverseiter(@a))
 	generic byreverse 	: (sl : @a[:] -> reverseiter(@a))
 ;;
 
@@ -66,18 +72,17 @@
 ;;
 
 generic byenum = {a
-	-> [.s = a, .idx = 0]
+	-> [.sl = a, .idx = 0]
 }
 
-impl iterable enumiter(@a) -> (size, @b) =
+impl iterable enumiter(@a) -> (size, @a) =
 	__iternext__ = {itp, valp
-		if itp.idx < itp.s.len
-			valp# = (itp.idx, itp.s[itp.idx])
-			itp.idx++
-			-> true
-		else
+		if itp.idx == itp.sl.len
 			-> false
 		;;
+		valp# = (itp.idx, itp.sl[itp.idx])
+		itp.idx++
+		-> true
 	}
 
 	__iterfin__ = {itp, valp
@@ -84,4 +89,21 @@
 	}
 ;;
 
+generic byref = {a
+	-> [.sl = a, .idx = 0]
+}
+
+impl iterable refiter(@a) -> @a# =
+	__iternext__ = {itp, valp
+		if itp.idx == itp.sl.len
+			-> false
+		;;
+		valp# = &itp.sl[itp.idx]
+		itp.idx++
+		-> true
+	}
+
+	__iterfin__ = {itp, valp
+	}
+;;