![JAR search and dependency download from the Maven repository](/logo.png)
net.kemitix.mon.combinator.Before Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mon Show documentation
Show all versions of mon Show documentation
TypeAlias, Result and Maybe for Java
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.mon.combinator;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Before pattern combinator.
*
* Original from http://boundsofjava.com/newsletter/003-introducing-combinators-part1
*
* @param the argument type
* @param the result type
*
* @author Federico Peralta Schaffner ([email protected])
*/
@FunctionalInterface
public interface Before extends
Function<
Consumer,
Function<
Function,
Function>> {
/**
* Decorates a function with a Consumer that will be supplied with the argument before applying it to the function.
*
* @param before the consumer that will receive the argument before the function
* @param function the function to apply the argument to and return the result value of
* @param the argument type
* @param the result type
*
* @return a partially applied Function that will take an argument and return the result of applying it to the
* function parameter
*/
public static Function decorate(
final Consumer before,
final Function function
) {
return Before.create().apply(before)
.apply(function);
}
/**
* Create a Before curried function.
*
* @param the argument type
* @param the result type
*
* @return a curried function that will pass the argument to before applying the supplied function
*/
public static Before create() {
return before -> function -> argument -> {
before.accept(argument);
return function.apply(argument);
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy