package com.github.davidmoten.rx;

import rx.Observable;
import rx.functions.Func1;

public final class Functions {

    private Functions() {
        // do nothing
    }

    public static <T> Func1<T, T> identity() {
        return new Func1<T, T>() {
            @Override
            public T call(T t) {
                return t;
            }
        };
    }

    public static <T> Func1<T, Boolean> alwaysTrue() {
        return new Func1<T, Boolean>() {
            @Override
            public Boolean call(T t) {
                return true;
            }
        };
    }

    public static <T> Func1<T, Boolean> alwaysFalse() {
        return new Func1<T, Boolean>() {
            @Override
            public Boolean call(T t) {
                return false;
            }
        };
    }

    public static <T, R> Func1<T, R> constant(final R r) {
        return new Func1<T, R>() {
            @Override
            public R call(T t) {
                return r;
            }
        };
    }

    public static <T> Func1<T, Boolean> not(final Func1<T, Boolean> f) {
        return new Func1<T, Boolean>() {
            @Override
            public Boolean call(T t) {
                return !f.call(t);
            }
        };
    }

    public static <T> Func1<T, Observable<T>> just() {
        return new Func1<T, Observable<T>>() {
            @Override
            public Observable<T> call(T t) {
                return Observable.just(t);
            }
        };
    }

}

@@ -76,16 +76,43 @@
 s.Func1;
+%0Aimport rx.functions.Func2;
 %0A%0Apublic
@@ -1518,10 +1518,1243 @@
 %0A    %7D%0A%0A
+    /**%0A     * Returns a Func2 that adds numbers. Useful for Observable.reduce but not%0A     * particularly performant as it does instanceOf checks.%0A     * %0A     * @return Func2 that adds numbers%0A     */%0A    public static %3CT extends Number%3E Func2%3CT, T, T%3E add() %7B%0A        return new Func2%3CT, T, T%3E() %7B%0A            @SuppressWarnings(%22unchecked%22)%0A            @Override%0A            public T call(T a, T b) %7B%0A                if (a instanceof Integer)%0A                    return (T) (Number) (a.intValue() + b.intValue());%0A                else if (a instanceof Long)%0A                    return (T) (Number) (a.longValue() + b.longValue());%0A                else if (a instanceof Double)%0A                    return (T) (Number) (a.doubleValue() + b.doubleValue());%0A                else if (a instanceof Float)%0A                    return (T) (Number) (a.floatValue() + b.floatValue());%0A                else if (a instanceof Byte)%0A                    return (T) (Number) (a.byteValue() + b.byteValue());%0A                else if (a instanceof Short)%0A                    return (T) (Number) (a.shortValue() + b.shortValue());%0A                else%0A                    throw new RuntimeException(%22not implemented%22);%0A            %7D%0A        %7D;%0A    %7D%0A
 %7D%0A

add add