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

cyclops.control.Writer Maven / Gradle / Ivy

The newest version!
package cyclops.control;

import com.oath.cyclops.hkt.Higher;
import com.oath.cyclops.hkt.Higher2;
import com.oath.cyclops.types.functor.Transformable;
import cyclops.function.Function3;
import cyclops.function.Function4;
import cyclops.function.Monoid;
import com.oath.cyclops.hkt.DataWitness.writer;
import cyclops.typeclasses.functor.Functor;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import cyclops.data.tuple.Tuple;
import cyclops.data.tuple.Tuple2;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.function.BiFunction;
import java.util.function.Function;

@AllArgsConstructor(access= AccessLevel.PRIVATE)
@Getter
public final class Writer implements Transformable, Iterable,Higher2, Serializable {
    private static final long serialVersionUID = 1L;

    private final Tuple2 value;
    private final Monoid monoid;

    public  Writer map(Function mapper) {
        return writer(mapper.apply(value._1()), value._2(), monoid);
    }

    public  R fold(BiFunction,? super Monoid,? extends R> fn){
        return fn.apply(value,monoid);
    }
    public  Writer flatMap(Function> fn) {
        Writer writer = fn.apply(value._1());
        return writer(writer.value._1(), writer.monoid.apply(value._2(), writer.value._2()), writer.monoid);
    }

    public Writer tell(W write){
        return writer(value._1(),monoid.apply(write,value._2()),monoid);
    }

    public  Writer set(R value){
            return writer(value,this.value._2(),monoid);
    }

    /*
     * Perform a For Comprehension over a Writer, accepting 3 generating function.
             * This results in a four level nested internal iteration over the provided Writers.
      *
              *  
      * {@code
      *
      *   import static com.oath.cyclops.reactor.Writers.forEach4;
      *
         forEach4(Writer.just(1),
                 a-> Writer.just(a+1),
                 (a,b) -> Writer.just(a+b),
                 a                  (a,b,c) -> Writer.just(a+b+c),
                 Tuple::tuple)
      *
      * }
      * 
* * @param value1 top level Writer * @param value2 Nested Writer * @param value3 Nested Writer * @param value4 Nested Writer * @param yieldingFunction Generates a result per combination * @return Writer with a combined value generated by the yielding function */ public Writer forEach4(Function> value2, BiFunction> value3, Function3> value4, Function4 yieldingFunction) { return this.flatMap(in -> { Writer a = value2.apply(in); return a.flatMap(ina -> { Writer b = value3.apply(in,ina); return b.flatMap(inb -> { Writer c = value4.apply(in,ina,inb); return c.map(in2 -> { return yieldingFunction.apply(in, ina, inb, in2); }); }); }); }); } /** * Perform a For Comprehension over a Writer, accepting 2 generating function. * This results in a three level nested internal iteration over the provided Writers. * *
     * {@code
     *
     *   import static com.oath.cyclops.reactor.Writers.forEach3;
     *
    forEach3(Writer.just(1),
    a-> Writer.just(a+1),
    (a,b) -> Writer.just(a+b),
    Tuple::tuple)
     *
     * }
     * 
* * @param value2 Nested Writer * @param value3 Nested Writer * @param yieldingFunction Generates a result per combination * @return Writer with a combined value generated by the yielding function */ public Writer forEach3(Function> value2, BiFunction> value3, Function3 yieldingFunction) { return this.flatMap(in -> { Writer a = value2.apply(in); return a.flatMap(ina -> { Writer b = value3.apply(in,ina); return b.map(in2 -> { return yieldingFunction.apply(in, ina, in2); }); }); }); } /** * Perform a For Comprehension over a Writer, accepting a generating function. * This results in a two level nested internal iteration over the provided Writers. * *
     * {@code
     *
     *   import static com.oath.cyclops.reactor.Writers.forEach;
     *
    forEach(Writer.just(1),
    a-> Writer.just(a+1),
    Tuple::tuple)
     *
     * }
     * 
* * @param value2 Nested Writer * @param yieldingFunction Generates a result per combination * @return Writer with a combined value generated by the yielding function */ public Writer forEach2(Function> value2, BiFunction yieldingFunction) { return this.flatMap(in -> { Writer a = value2.apply(in); return a.map(in2 -> { return yieldingFunction.apply(in, in2); }); }); } public static Writer writer(T value, Monoid combiner) { return new Writer(Tuple.tuple(value, combiner.zero()), combiner); } public static Writer writer(T value, W initial, Monoid combiner) { return new Writer(Tuple.tuple(value, initial), combiner); } public static Writer writer(Tuple2 values, Monoid combiner) { return new Writer(values, combiner); } @Override public Iterator iterator() { return Arrays.asList(value._1()).iterator(); } public static Writer narrowK2(final Higher2 t) { return (Writer)t; } public static Writer narrowK(final Higher,T> t) { return (Writer)t; } public static Higher, T> widen(Writer narrow) { return narrow; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy