Explorar o código

Add attempt_where

Billy Barrow hai 6 días
pai
achega
fed62f42b4
Modificáronse 2 ficheiros con 12 adicións e 0 borrados
  1. 1 0
      src/lib/Attempt.vala
  2. 11 0
      src/lib/Enumerable.vala

+ 1 - 0
src/lib/Attempt.vala

@@ -2,6 +2,7 @@ namespace Invercargill {
 
     public delegate T AttemptDelegate<T>() throws Error;
     public delegate TOut AttemptTransformDelegate<TIn, TOut>(TIn input) throws Error;
+    public delegate bool AttemptPredicate<T>(T item) throws Error;
 
     [Compact]
     [Immutable]

+ 11 - 0
src/lib/Enumerable.vala

@@ -166,6 +166,17 @@ namespace Invercargill {
                 .assert_promotion<Attempts>();
         }
 
+        public virtual Attempts<T> attempt_where(owned AttemptPredicate<T> predicate) {
+            return select_where<Attempt<T>>((i, ref o) => {
+                bool match = true;
+                o = new Attempt<T>(() => {
+                    match = predicate(i);
+                    return i;
+                });
+                return match;
+            }).assert_promotion<Attempts>();
+        }
+
         public virtual Enumerable<Pair<TFirst, TSecond>> select_pairs<TFirst, TSecond>(owned TransformDelegate<T, TFirst> transform1, owned TransformDelegate<T, TSecond> transform2) {
             return select<Pair<TFirst, TSecond>>(i => new Pair<TFirst, TSecond>(transform1(i), true, transform2(i), true));
         }