org.rapidpm.frp.Transformations Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rapidpm-functional-reactive Show documentation
Show all versions of rapidpm-functional-reactive Show documentation
Functional Reactive with Core Java
/**
* Copyright © 2017 Sven Ruppert ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.rapidpm.frp;
import org.rapidpm.frp.functions.CheckedBiFunction;
import org.rapidpm.frp.functions.CheckedFunction;
import org.rapidpm.frp.functions.CheckedTriFunction;
import org.rapidpm.frp.functions.TriFunction;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* Created by svenruppert on 24.04.17.
*
* @author svenruppert
* @version $Id: $Id
*/
public interface Transformations {
/**
* not.
*
* @return a {@link java.util.function.Function} object.
*/
static Function not() {
return (input) -> !input;
}
/**
* higherCompose.
*
* @param a T object.
* @param a U object.
* @param a V object.
* @return a {@link java.util.function.Function} object.
*/
static Function, Function, Function>> higherCompose() {
return (Function f) -> (Function g) -> (T x) -> f.apply(g.apply(x));
}
/**
* enumToStream.
*
* @param a T object.
* @return a {@link java.util.function.Function} object.
*/
static Function, Stream> enumToStream() {
return (e) ->
StreamSupport
.stream(Spliterators.spliteratorUnknownSize(new Iterator() {
public T next() {
return e.nextElement();
}
public boolean hasNext() {
return e.hasMoreElements();
}
}, Spliterator.ORDERED), false);
}
/**
* curryBiFunction.
*
* @param a A object.
* @param a B object.
* @param a R object.
* @return a {@link java.util.function.Function} object.
*/
static Function, Function>> curryBiFunction() {
return (func) -> a -> b -> func.apply(a, b);
}
/**
* curryCheckedBiFunction.
*
* @param a A object.
* @param a B object.
* @param a R object.
* @return a {@link java.util.function.Function} object.
*/
static Function, Function>> curryCheckedBiFunction() {
return (func) -> a -> b -> func.applyWithException(a, b);
}
/**
* unCurryBifunction.
*
* @param a A object.
* @param a B object.
* @param a R object.
* @return a {@link java.util.function.Function} object.
*/
static Function>, BiFunction> unCurryBiFunction() {
return (func) -> (a, b) -> func.apply(a).apply(b);
}
/**
* unCurryCheckedBifunction.
*
* @param a A object.
* @param a B object.
* @param a R object.
* @return a {@link java.util.function.Function} object.
*/
static Function>, CheckedBiFunction> unCurryCheckedBiFunction() {
return (func) -> (a, b) -> func.apply(a).applyWithException(b);
}
/**
* curryTriFunction.
*
* @param a A object.
* @param a B object.
* @param a C object.
* @param a R object.
* @return a {@link java.util.function.Function} object.
*/
static Function<
TriFunction,
Function>>> curryTriFunction() {
return (func) -> a -> b -> c -> func.apply(a, b, c);
}
/**
* curryCheckedTriFunction.
*
* @param a A object.
* @param a B object.
* @param a C object.
* @param a R object.
* @return a {@link java.util.function.Function} object.
*/
static Function<
CheckedTriFunction,
Function>>> curryCheckedTriFunction() {
return (func) -> a -> b -> c -> func.applyWithException(a, b, c);
}
/**
* unCurryTrifunction.
*
* @param a A object.
* @param a B object.
* @param a C object.
* @param a R object.
* @return a {@link java.util.function.Function} object.
*/
static Function<
Function>>,
TriFunction> unCurryTriFunction() {
return (func) -> (a, b, c) -> func.apply(a).apply(b).apply(c);
}
/**
* unCurryTrifunction.
*
* @param a A object.
* @param a B object.
* @param a C object.
* @param a R object.
* @return a {@link java.util.function.Function} object.
*/
static Function<
Function>>,
CheckedTriFunction> unCurryCheckedTriFunction() {
return (func) -> (a, b, c) -> func.apply(a).apply(b).applyWithException(c);
}
//Function Casts
/**
* not.
*
* @param p a {@link java.util.function.Predicate} object.
* @param a T object.
* @return a {@link java.util.function.Predicate} object.
*/
static Predicate not(Predicate p) {
return t -> !p.test(t);
}
/**
* asPredicate.
*
* @param predicate a {@link java.util.function.Predicate} object.
* @param a T object.
* @return a {@link java.util.function.Predicate} object.
*/
static Predicate asPredicate(Predicate predicate) {
return predicate;
}
/**
* asConsumer.
*
* @param consumer a {@link java.util.function.Consumer} object.
* @param a T object.
* @return a {@link java.util.function.Consumer} object.
*/
static Consumer asConsumer(Consumer consumer) {
return consumer;
}
/**
* asSupplier.
*
* @param supplier a {@link java.util.function.Supplier} object.
* @param a T object.
* @return a {@link java.util.function.Supplier} object.
*/
static Supplier asSupplier(Supplier supplier) {
return supplier;
}
/**
* asFunc.
*
* @param function a {@link java.util.function.Function} object.
* @param a T object.
* @param a R object.
* @return a {@link java.util.function.Function} object.
*/
static Function asFunc(Function function) {
return function;
}
static CheckedFunction asCheckedFunc(Function f) {
return f::apply;
}
}