All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cyclops.control.State Maven / Gradle / Ivy

The newest version!
package cyclops.control;

import com.oath.cyclops.hkt.Higher;
import com.oath.cyclops.hkt.Higher2;
import cyclops.control.Maybe.Nothing;
import com.oath.cyclops.hkt.DataWitness.state;
import com.oath.cyclops.hkt.DataWitness.supplier;
import cyclops.free.Free;
import cyclops.function.*;
import cyclops.kinds.SupplierKind;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import cyclops.data.tuple.Tuple;
import cyclops.data.tuple.Tuple2;

import java.util.function.BiFunction;
import java.util.function.Function;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public final class State implements Higher2 {

    private final Function1>> runState;

    public Tuple2 run(S s) {
        return SupplierKind.run(runState.apply(s));
    }
    public T eval(S s) {
        return SupplierKind.run(runState.apply(s))._2();
    }
    public static  State get() {
        return state(s -> Tuple.tuple(s, s));
    }

    public static  State transition(Function f) {
        return state(s -> Tuple.tuple(f.apply(s),(Nothing) Maybe.nothing()));
    }

    public static  State transition(Function f, T value) {
        return state(s -> Tuple.tuple(f.apply(s),value));
    }
    public  State combine(State combine, BiFunction combiner) {
        return flatMap(a -> combine.map(b -> combiner.apply(a,b)));
    }

    public  State map(Function mapper) {
        return mapState(t -> Tuple.tuple(t._1(), mapper.apply(t._2())));
    }
    public  State mapState(Function, Tuple2> fn) {
        return suspended(s -> runState.apply(s).map(t -> fn.apply(t)));
    }
    private static  State suspended(Function1>> runF) {
        return new State<>(s -> SupplierKind.suspend(SupplierKind.λK(()->runF.apply(s))));
    }

    public  State flatMap(Function> f) {
        return suspended(s -> runState.apply(s).flatMap(t -> Free.done(f.apply(t._2()).run(t._1()))));
    }
    public static  State constant(T constant) {
        return state(s -> Tuple.tuple(s, constant));
    }

    /*
  * Perform a For Comprehension over a State, accepting 3 generating function.
          * This results in a four level nested internal iteration over the provided States.
   *
           *  
   * {@code
   *
   *   import static com.oath.cyclops.reactor.States.forEach4;
   *
      forEach4(State.just(1),
              a-> State.just(a+1),
              (a,b) -> State.just(a+b),
              a                  (a,b,c) -> State.just(a+b+c),
              Tuple::tuple)
   *
   * }
   * 
* * @param value1 top level State * @param value2 Nested State * @param value3 Nested State * @param value4 Nested State * @param yieldingFunction Generates a result per combination * @return State with a combined value generated by the yielding function */ public State forEach4(Function> value2, BiFunction> value3, Function3> value4, Function4 yieldingFunction) { return this.flatMap(in -> { State a = value2.apply(in); return a.flatMap(ina -> { State b = value3.apply(in,ina); return b.flatMap(inb -> { State c = value4.apply(in,ina,inb); return c.map(in2 -> { return yieldingFunction.apply(in, ina, inb, in2); }); }); }); }); } /** * Perform a For Comprehension over a State, accepting 2 generating function. * This results in a three level nested internal iteration over the provided States. * *
     * {@code
     *
     *   import static com.oath.cyclops.reactor.States.forEach3;
     *
    forEach3(State.just(1),
    a-> State.just(a+1),
    (a,b) -> State.just(a+b),
    Tuple::tuple)
     *
     * }
     * 
* * @param value2 Nested State * @param value3 Nested State * @param yieldingFunction Generates a result per combination * @return State with a combined value generated by the yielding function */ public State forEach3(Function> value2, BiFunction> value3, Function3 yieldingFunction) { return this.flatMap(in -> { State a = value2.apply(in); return a.flatMap(ina -> { State b = value3.apply(in,ina); return b.map(in2 -> { return yieldingFunction.apply(in, ina, in2); }); }); }); } /** * Perform a For Comprehension over a State, accepting a generating function. * This results in a two level nested internal iteration over the provided States. * *
     * {@code
     *
     *   import static com.oath.cyclops.reactor.States.forEach;
     *
    forEach(State.just(1),
    a-> State.just(a+1),
    Tuple::tuple)
     *
     * }
     * 
* * @param value2 Nested State * @param yieldingFunction Generates a result per combination * @return State with a combined value generated by the yielding function */ public State forEach2(Function> value2, BiFunction yieldingFunction) { return this.flatMap(in -> { State a = value2.apply(in); return a.map(in2 -> { return yieldingFunction.apply(in, in2); }); }); } public static State tailRec(T initial, Function>> fn) { return fn.apply(initial).flatMap( eval -> eval.fold(s->narrowK(tailRec(s,fn)), p->State.constant(p))); } public static State state(Function> runF) { return new State<>(s -> Free.done(runF.apply(s))); } public static State of(S s) { return state(__ -> Tuple.tuple(s, (Nothing)Maybe.nothing())); } public static State put(S s) { return of(s); } public static State narrowK2(final Higher2 t) { return (State)t; } public static State narrowK(final Higher,T> t) { return (State)t; } public static Higher, T> widen(State narrow) { return narrow; } }