[libgee] Preserve laziness in Stream



commit 34b6a361dde07095df640f1e616233b8af2af02e
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Mon Jul 25 03:13:47 2011 +0100

    Preserve laziness in Stream

 gee/iterator.vala    |    4 ++--
 gee/traversable.vala |   15 ++++++++++++---
 2 files changed, 14 insertions(+), 5 deletions(-)
---
diff --git a/gee/iterator.vala b/gee/iterator.vala
index f3b74d1..aecd3c0 100644
--- a/gee/iterator.vala
+++ b/gee/iterator.vala
@@ -86,7 +86,7 @@ public interface Gee.Iterator<G> : Object, Traversable<G> {
 		switch (str) {
 		case Traversable.Stream.CONTINUE:
 			if (self.valid) {
-				str = f (Traversable.Stream.CONTINUE, self.get (), out initial);
+				str = f (Traversable.Stream.CONTINUE, new Lazy<G> (() => {return self.get ();}), out initial);
 				switch (str) {
 				case Traversable.Stream.YIELD:
 				case Traversable.Stream.CONTINUE:
@@ -120,7 +120,7 @@ public interface Gee.Iterator<G> : Object, Traversable<G> {
 				} else {
 					need_next = true;
 				}
-				str = f (Traversable.Stream.CONTINUE, self.get (), out val);
+				str = f (Traversable.Stream.CONTINUE, new Lazy<G> (() => {return self.get ();}), out val);
 			}
 			switch (str) {
 			case Traversable.Stream.YIELD:
diff --git a/gee/traversable.vala b/gee/traversable.vala
index 016130e..d90dd9e 100644
--- a/gee/traversable.vala
+++ b/gee/traversable.vala
@@ -26,7 +26,7 @@ namespace Gee {
 	public delegate A FoldFunc<A, G> (owned G g, owned A a);
 	public delegate void ForallFunc<G> (owned G g);
 	public delegate Lazy<A>? UnfoldFunc<A> ();
-	public delegate Traversable.Stream StreamFunc<G, A> (Traversable.Stream state, owned G? g, out Lazy<A>? lazy);
+	public delegate Traversable.Stream StreamFunc<G, A> (Traversable.Stream state, owned Lazy<G>? g, out Lazy<A>? lazy);
 	public delegate A MapFunc<A, G> (owned G g);
 }
 
@@ -111,7 +111,11 @@ public interface Gee.Traversable<G> : Object
 			case Stream.YIELD:
 				return Stream.CONTINUE;
 			case Stream.CONTINUE:
-				val = new Lazy<A>(() => {return (f ((owned)item));});
+				val = new Lazy<A>(() => {
+					A tmp = item.get ();
+					item = null;
+					return (f ((owned)tmp));
+				});
 				return Stream.YIELD;
 			case Stream.END:
 				return Stream.END;
@@ -145,7 +149,12 @@ public interface Gee.Traversable<G> : Object
 					return Stream.YIELD;
 				}
 			case Stream.CONTINUE:
-				val = new Lazy<A> (() => {seed = f ((owned)item, (owned) seed); return seed;});
+				val = new Lazy<A> (() => {
+					A tmp = item.get ();
+					item = null;
+					seed = f ((owned) tmp, (owned) seed);
+					return seed;
+				});
 				return Stream.YIELD;
 			case Stream.END:
 				return Stream.END;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]