Pārlūkot izejas kodu

More breaking changes to make attempts good

Billy Barrow 1 mēnesi atpakaļ
vecāks
revīzija
3757431e05

+ 10 - 5
src/lib/Enumerable.vala

@@ -104,13 +104,14 @@ namespace Invercargill {
         }
 
         public virtual Attempts<Tout> try_select<Tout>(owned AttemptTransformDelegate<T, Tout> transform) {
-            return select<Attempt<Tout>>(i => new Attempt<Tout>(() => transform(i))).promote_to<Attempts>();
+            return select<Attempt<Tout>>(i => new Attempt<Tout>(() => transform(i))).assert_promotion<Attempts>();
         }
 
         public virtual Attempts<Tout> try_select_nested<Tout>(owned AttemptTransformDelegate<T, Enumerable<Attempt<Tout>>> transform) {
             return try_select<Enumerable<Attempt<Tout>>>((owned)transform)
+                .as_enumerable()
                 .select_many<Attempt<Tout>>(a => a.success ? a.result : Invercargill.single<Attempt<Tout>>(new Attempt<Tout>.unsuccessful(a.error)))
-                .promote_to<Attempts>();
+                .assert_promotion<Attempts>();
         }
 
         public virtual Enumerable<Pair<TFirst, TSecond>> select_pairs<TFirst, TSecond>(owned TransformDelegate<T, TFirst> transform1, owned TransformDelegate<T, TSecond> transform2) {
@@ -363,7 +364,7 @@ namespace Invercargill {
             return typeof(T);
         }}
 
-        public virtual TPromotion try_promote_to<TPromotion>() throws PromotionError {
+        public virtual TPromotion promote_to<TPromotion>() throws PromotionError {
             var type = typeof(TPromotion);
             if(get_type().is_a(type)) {
                 // Don't promote if we are already target type
@@ -390,9 +391,9 @@ namespace Invercargill {
             return ((Promotion)promotion).wrap(this);
         }
 
-        public virtual TPromotion promote_to<TPromotion>() {
+        public virtual TPromotion assert_promotion<TPromotion>() {
             try {
-                return try_promote_to<TPromotion>();
+                return promote_to<TPromotion>();
             }
             catch (PromotionError error) {
                 var base_type = get_type();
@@ -453,6 +454,10 @@ namespace Invercargill {
             return try_select<Tout>(o => mapper.materialise(o));
         }
 
+        public virtual Enumerable<T> as_enumerable() {
+            return this;
+        }
+
         private PredicateDelegate<T> resolve_nullable_predicate(PredicateDelegate<T>? predicate) {
             if(predicate == null) {
                 return (p) => true;

+ 12 - 4
src/lib/EnumerableProxy.vala

@@ -175,12 +175,12 @@ namespace Invercargill {
             }
         }
     
-        public override TPromotion try_promote_to<TPromotion>() throws PromotionError {
-            return inner.try_promote_to<TPromotion>();
+        public override TPromotion promote_to<TPromotion>() throws PromotionError {
+            return inner.promote_to<TPromotion>();
         }
     
-        public override TPromotion promote_to<TPromotion>() {
-            return inner.promote_to<TPromotion>();
+        public override TPromotion assert_promotion<TPromotion>() {
+            return inner.assert_promotion<TPromotion>();
         }
 
         public override Enumerable<T> seal() {
@@ -217,6 +217,10 @@ namespace Invercargill {
             return inner.try_select<Tout>((owned)transform);
         }
 
+        public override Attempts<Tout> try_select_nested<Tout>(owned AttemptTransformDelegate<T, Enumerable<Attempt<Tout>>> transform) {
+            return inner.try_select_nested<Tout>((owned)transform);
+        }   
+
         public override Enumerable<T> cache() {
             return inner.cache();
         }
@@ -224,6 +228,10 @@ namespace Invercargill {
         public override Attempts<Tout> try_map_with<Tout>(Mapper<Tout, T> mapper) {
             return inner.try_map_with<Tout>(mapper);
         }
+        
+        public override Enumerable<T> as_enumerable() {
+            return inner.as_enumerable();
+        }
 
     }
 

+ 35 - 28
src/lib/Promotions/AttemptEnumerable.vala

@@ -19,7 +19,7 @@ namespace Invercargill {
             return inner.all(a => a.success);
         }
 
-        public bool unwrap_iterate_if(PredicateDelegate<T> handler) throws Error {
+        public new bool iterate_if(PredicateDelegate<T> handler) throws Error {
             var tracker = get_tracker();
             while(tracker.has_next()) {
                 var item = tracker.get_next();
@@ -35,26 +35,26 @@ namespace Invercargill {
             return true;
         }
 
-        public void unwrap_iterate(ItemDelegate<T> handler) throws Error {
-            unwrap_iterate_if(i => {
+        public new void iterate(ItemDelegate<T> handler) throws Error {
+            iterate_if(i => {
                 handler(i);
                 return true;
             });
         }
 
-        public Series<T> unwrap_to_series() throws Error {
+        public new Series<T> to_series() throws Error {
             var series = new Series<T>();
-            unwrap_iterate(i => series.add(i));
+            iterate(i => series.add(i));
             return series;
         }
 
-        public Gee.Collection<T> unwrap_to_gee_collection() throws Error {
+        public new Gee.Collection<T> to_gee_collection() throws Error {
             var collection = new Gee.LinkedList<T>();
-            unwrap_iterate(i => collection.add(i));
+            iterate(i => collection.add(i));
             return collection;
         }
 
-        public virtual T[] unwrap_to_array() throws Error {
+        public new T[] to_array() throws Error {
             var array = new T[1024];
             var index = 0;
             foreach (var item in this) {
@@ -71,43 +71,43 @@ namespace Invercargill {
             return array;
         }
 
-        public Vector<T> unwrap_to_vector() throws Error {
+        public new Vector<T> to_vector() throws Error {
             var vector = new Vector<T>();
-            unwrap_iterate(i => vector.add(i));
+            iterate(i => vector.add(i));
             return vector;
         }
 
-        public Dictionary<TKey, T> unwrap_to_dictionary<TKey>(TransformDelegate<T, TKey> key_selecter, HashFunc<TKey>? key_hash_func = null, EqualFunc<TKey>? key_equal_func = null) throws Error {
+        public new Dictionary<TKey, T> to_dictionary<TKey>(TransformDelegate<T, TKey> key_selecter, HashFunc<TKey>? key_hash_func = null, EqualFunc<TKey>? key_equal_func = null) throws Error {
             var dict = new Dictionary<TKey, T>(key_hash_func, key_equal_func);       
-            unwrap_iterate(i => dict.set(key_selecter(i), i));
+            iterate(i => dict.set(key_selecter(i), i));
             return dict;
         }
 
-        public Dictionary<TKey, TValue> unwrap_select_to_dictionary<TKey, TValue>(TransformDelegate<T, TKey> key_selecter, TransformDelegate<T, TValue> value_selecter, HashFunc<TKey>? key_hash_func = null, EqualFunc<TKey>? key_equal_func = null) throws Error {
+        public new Dictionary<TKey, TValue> select_to_dictionary<TKey, TValue>(TransformDelegate<T, TKey> key_selecter, TransformDelegate<T, TValue> value_selecter, HashFunc<TKey>? key_hash_func = null, EqualFunc<TKey>? key_equal_func = null) throws Error {
             var dict = new Dictionary<TKey, T>(key_hash_func, key_equal_func);       
-            unwrap_iterate(i => dict.set(key_selecter(i), value_selecter(i)));
+            iterate(i => dict.set(key_selecter(i), value_selecter(i)));
             return dict;
         }
 
-        public Set<T> unwrap_to_set(HashFunc<T>? hash_func = null, EqualFunc<T>? equal_func = null) throws Error {
+        public new Set<T> to_set(HashFunc<T>? hash_func = null, EqualFunc<T>? equal_func = null) throws Error {
             var @set = new Set<T>(hash_func, equal_func);       
-            unwrap_iterate(i => @set.add(i));
+            iterate(i => @set.add(i));
             return @set;
         }
 
-        public Elements unwrap_to_elements() throws Error {
+        public new Elements to_elements() throws Error {
             var series = new ElementSeries();
             if(typeof(T).is_a(typeof(Element))) {
-                unwrap_iterate(i => series.add((Element)i));
+                iterate(i => series.add((Element)i));
             }
             else {
-                unwrap_iterate(i => series.add(new NativeElement<T>(i)));
+                iterate(i => series.add(new NativeElement<T>(i)));
             }
             return series;
         }
 
-        public Object[] unwrap_to_object_array() throws Error {
-            return unwrap_to_vector().to_object_array();
+        public new Object[] to_object_array() throws Error {
+            return to_vector().to_object_array();
         }
 
 
@@ -115,7 +115,7 @@ namespace Invercargill {
         public new bool any(PredicateDelegate<T> predicate = (i) => true) throws Error {
             var result = false;
             var p = resolve_nullable_predicate(predicate);
-            unwrap_iterate_if(i => {
+            iterate_if(i => {
                 if(p(i)) {
                     result = true;
                     return false;
@@ -136,19 +136,19 @@ namespace Invercargill {
         }
 
         public new Attempts<T> where(owned PredicateDelegate<T> predicate) {
-            return inner.where(i => !i.success || predicate(i)).promote_to<Attempts<T>>();
+            return inner.where(i => !i.success || predicate(i)).assert_promotion<Attempts<T>>();
         }
     
         public new Attempts<Tout> select<Tout>(owned TransformDelegate<T, Tout> transform) {
-            return inner.select<Attempt<Tout>>(a => a.success ? new Attempt<Tout>.successful(transform(a.result)) : new Attempt<Tout>.unsuccessful(a.error)).promote_to<Attempts<T>>();
+            return inner.select<Attempt<Tout>>(a => a.success ? new Attempt<Tout>.successful(transform(a.result)) : new Attempt<Tout>.unsuccessful(a.error)).assert_promotion<Attempts<T>>();
         }
     
         public new Attempts<Tout> select_many<Tout>(owned TransformDelegate<T, Enumerable<Tout>> transform) {
-            return inner.select_many<Attempt<Tout>>(a => a.success ? transform(a.result).select<Attempt<Tout>>(i => new Attempt<Tout>.successful(i)) : Invercargill.single<Attempt<Tout>>(new Attempt<Tout>.unsuccessful(a.error))).promote_to<Attempts<T>>();
+            return inner.select_many<Attempt<Tout>>(a => a.success ? transform(a.result).select<Attempt<Tout>>(i => new Attempt<Tout>.successful(i)) : Invercargill.single<Attempt<Tout>>(new Attempt<Tout>.unsuccessful(a.error))).assert_promotion<Attempts<T>>();
         }
     
         public new Attempts<T> concat(Enumerable<Attempt<T>> other) {
-            return inner.concat(other).promote_to<Attempts>();
+            return inner.concat(other).assert_promotion<Attempts>();
         }
     
         public new Attempts<Tout> cast<Tout>() {
@@ -164,7 +164,7 @@ namespace Invercargill {
     
         public new Tout aggrigate<Tout>(Tout initial, AggrigateDelegate<Tout, T> aggrigate_func) throws Error {
             var aggrigate = initial;
-            unwrap_iterate(i => {
+            iterate(i => {
                 aggrigate = aggrigate_func(aggrigate, i);
             });
             return aggrigate;
@@ -225,13 +225,20 @@ namespace Invercargill {
         }
     
         public new Attempts<Tout> select_where<Tout>(owned FilterTransformDelegate<T, Tout> transform) {
-            return inner.select_where<Tout>((a, ref o) => !a.success || transform(a.result, ref o)).promote_to<Attempts<Tout>>();
+            return inner.select_where<Tout>((a, ref o) => !a.success || transform(a.result, ref o)).assert_promotion<Attempts<Tout>>();
         }
 
         public new Attempts<Tout> try_select<Tout>(owned AttemptTransformDelegate<T, Tout> transform) {
             return inner.try_select<Tout>(a => a.success ? new Attempt<Tout>(() => transform(a.result)) : new Attempt<Tout>.unsuccessful(a.error));
         }
 
+        public new Attempts<Tout> try_select_nested<Tout>(owned AttemptTransformDelegate<T, Enumerable<Attempt<Tout>>> transform) {
+            return try_select<Enumerable<Attempt<Tout>>>((owned)transform)
+                .as_enumerable()
+                .select_many<Attempt<Tout>>(a => a.success ? a.result : Invercargill.single<Attempt<Tout>>(new Attempt<Tout>.unsuccessful(a.error)))
+                .assert_promotion<Attempts<Tout>>();
+        }
+
         public new Attempts<Tout> try_map_with<Tout>(Mapper<Tout, T> mapper) {
             return try_select<Tout>(o => mapper.materialise(o));
         }

+ 1 - 1
src/tests/Integration/Cache.vala

@@ -6,7 +6,7 @@ void cache_tests() {
 
     Test.add_func("/invercargill/operator/cache", () => {
         var runs = 0;
-        var enumerable = range(0, 64).act(() => runs++).cache().promote_to<SignedNativeIntegers>();
+        var enumerable = range(0, 64).act(() => runs++).cache().assert_promotion<SignedNativeIntegers>();
 
         var count = enumerable.count();
         var min = enumerable.min();

+ 2 - 2
src/tests/Integration/Numbers.vala

@@ -6,7 +6,7 @@ void numbers_test() {
     Test.add_func("/invercargill/numbers/average", () => {
 
         var data = range(1, 60, 1).select<double?>(i => (double?)i);
-        var numbers = data.promote_to<Doubles>();
+        var numbers = data.assert_promotion<Doubles>();
         var average = numbers.average();
 
         assert_cmpfloat(average, CompareOperator.EQ, 30);
@@ -20,7 +20,7 @@ void numbers_test() {
         data.add(15);
         data.add(10);
 
-        var numbers = data.promote_to<SignedNativeIntegers>();
+        var numbers = data.assert_promotion<SignedNativeIntegers>();
         var average = numbers.average();
 
         assert_cmpfloat(average, CompareOperator.EQ, 10);

+ 2 - 2
src/tests/Integration/Promotion.vala

@@ -10,7 +10,7 @@ void promotion_tests() {
         data.add(9);
         data.add(7);
 
-        var base64 = data.promote_to<BinaryData>().to_base64();
+        var base64 = data.assert_promotion<BinaryData>().to_base64();
         
         assert_cmpstr("AQAA", CompareOperator.EQ, base64);
 
@@ -19,7 +19,7 @@ void promotion_tests() {
     Test.add_func("/invercargill/promotions/binarydata_to_binarydata", () => {
 
         var data = (Enumerable<uint8>)new BinaryData.from_base64("AQAA");
-        var new_data = data.promote_to<BinaryData>();
+        var new_data = data.assert_promotion<BinaryData>();
         
         assert_true(data == new_data);
     });