I'm trying to experiment with lambdas for fun. I created a functor which allows the composition of a lambda. But, the means of composition only allow a linear transformation, and does not allow branching.
The idea is that I know I will have, in the future, an effectively immutable state data structure. I want to compose a transform that will extract a value from the state; and will perform a series of steps, which may or may not require the state, to perform the transform.
To this end, I create two classes. The functional interface which works like java.util.function.Function, but takes a BiFunction in the andThen method, which allows the state parameter to be passed from lambda to lambda.
import java.util.Objects;
import java.util.function.BiFunction;
@FunctionalInterface
public interface Procedure<S, T> {
T procede(S stateStructure);
default <R> Procedure<S, R> andThen(BiFunction<S, T, R> after) {
Objects.requireNonNull(after);
return (param) -> after.apply(param, procede(param));
}
}
The functor is fairly straightforward, having two mapping functions (one which utilizes the state, and one which doesn't), and two terminating methods that finalize the transformations (again, with and without state).
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
public class ProcedureContainer<S, T> {
protected final Procedure<S, T> procedure;
protected ProcedureContainer(final Procedure<S, T> procedure) {
this.procedure = procedure;
}
public static <S, R> ProcedureContainer<S, R> initializeContainer(
final Function<S, R> initialDataRetriever) {
return new ProcedureContainer<>(initialDataRetriever::apply);
}
public <R> ProcedureContainer<S, R> map(final BiFunction<S, T, R> mapper) {
return new ProcedureContainer<>(procedure.andThen(mapper));
}
public <R> ProcedureContainer<S, R> map(final Function<T, R> mapper) {
BiFunction<S, T, R> subMapper =
(ignored, stagedData) -> mapper.apply(stagedData);
return new ProcedureContainer<>(procedure.andThen(subMapper));
}
public Consumer<S> terminate(final BiConsumer<S, T> consumer) {
return (param) -> consumer.accept(param, procedure.procede(param));
}
public Consumer<S> terminate(final Consumer<T> consumer) {
return (param) -> consumer.accept(procedure.procede(param));
}
}
A brief (contrived) example:
StateStruct state = new StateStruct();
state.setJson("{\"data\":\"meow, meow, I'm a cow\"}");
state.setRequestedField("data");
Consumer<StateStruct> consumer = ProcedureContainer
.initializeContainer(SateStruct::getJson)
.map(JSONObject::new)
.map((state, jsonObj) -> jsonObject.getString(state.getRequsetedField()))
.terminate(System.out::singLoudly);
consumer.accept(state);
Does anyone have any ideas of how I could implement a branch method on the ProcedureContainer that would allow a conditional branch in the execution of the final consumer. I'm thinking something that would make this example work:
StateStruct state = new StateStruct();
state.setJson("{\"data\":\"meow, meow, I'm a cow\"}");
state.setRequestedField("data");
state.setDefaultMessage("There is no data... only sheep");
Consumer<StateStruct> consumer = ProcedureContainer
.initializeContainer(SateStruct::getJson)
.map(JSONObject::new)
.branch((state, jsonObj) -> !jsonObject.getString(state.getRequsetedField()))
.terminateBranch((state, json) -> System.out.lament(state.getDefaultMessage()))
.map((state, jsonObj) -> jsonObject.getString(state.getRequsetedField()))
.terminate(System.out::singLoudly);
consumer.accept(state);
I've attempted by creating a new BranchProcedureContainer, which has a map and terminateBranch method. This issue is that I don't know how to merge the two branches in such a way that only the branch gets run.
There are no restrictions on creating new classes, or adding methods to existing classes.