Commits

Sergei Vavinov committed 7b8646d

Function[12][IB] memoization

Comments (0)

Files changed (4)

src/main/java/ru/yandex/bolts/function/Function1B.java

         };
     }
 
+    public static <A> Function1B<A> asFunction1B(final Function<A, Boolean> f) {
+        return new Function1B<A>() {
+            public boolean apply(A a) {
+                return f.apply(a);
+            }
+
+            @Override
+            public String toString() {
+                return f.toString();
+            }
+        };
+    }
+
     @SuppressWarnings("unchecked")
     public <B> Function1B<B> uncheckedCast() {
         return (Function1B<B>) this;
         };
     }
 
+    public Function1B<A> memoize() {
+        return asFunction1B(asFunction().memoize());
+    }
+
 } //~

src/main/java/ru/yandex/bolts/function/Function1I.java

         };
     }
     
+    public static <A> Function1I<A> asFunction1I(final Function<A, Integer> f) {
+        return new Function1I<A>() {
+            public int apply(A a) {
+                return f.apply(a);
+            }
+            
+            @Override
+            public String toString() {
+                return f.toString();
+            }
+        };
+    }
+    
     @Override
     public int compareTo(A o) {
         return apply(o);
         };
     }
 
+    public Function1I<A> memoize() {
+        return asFunction1I(asFunction().memoize());
+    }
+    
 } //~

src/main/java/ru/yandex/bolts/function/Function2B.java

         };
     }
 
+    public static <A, B> Function2B<A, B> asFunction2B(final Function<Tuple2<A, B>, Boolean> f) {
+        return new Function2B<A, B>() {
+            public boolean apply(A a, B b) {
+                return f.apply(Tuple2.tuple(a, b));
+            }
+
+            @Override
+            public String toString() {
+                return f.toString();
+            }
+        };
+    }
+
     public Function2B<A, B> notF() {
         return new Function2B<A, B>() {
             public boolean apply(A a, B b) {
         return a == null || b == null ? a == b : a.equals(b);
     }
 
+    public Function2B<A, B> memoize() {
+        return asFunction2B(asFunction().memoize());
+    }
+
 } //~

src/main/java/ru/yandex/bolts/function/Function2I.java

         };
     }
 
+    public static <A, B> Function2I<A, B> asFunction2I(final Function<Tuple2<A, B>, Integer> f) {
+        return new Function2I<A, B>() {
+            public int apply(A a, B b) {
+                return f.apply(Tuple2.tuple(a, b));
+            }
+            
+            @Override
+            public String toString() {
+                return f.toString();
+            }
+        };
+    }
 
     /** Invert current comparator */
     public Function2I<B, A> invert() {
         return (Function2I<C, D>) this;
     }
 
+    public Function2I<A, B> memoize() {
+        return asFunction2I(asFunction().memoize());
+    }
+
 } //~