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

cyclops.control.Identity Maven / Gradle / Ivy

The newest version!
package cyclops.control;

import com.oath.cyclops.hkt.Higher;
import com.oath.cyclops.matching.Deconstruct;
import cyclops.data.tuple.*;
import com.oath.cyclops.hkt.DataWitness.identity;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

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)
public final class Identity implements Higher, Iterable, Deconstruct.Deconstruct1, Serializable {
    private static final long serialVersionUID = 1L;
    public final T value;


    public static   Identity tailRec(T initial, Function>> fn){
        Identity> next[] = new Identity[1];
        next[0] = Identity.of(Either.left(initial));
        boolean cont = true;
        do {

            cont = next[0].fold(p -> p.fold(s -> {
                next[0] = narrowK(fn.apply(s));
                return true;
            }, __ -> false));
        } while (cont);
        return next[0].map(x->x.fold(l->null, r->r));
    }
    public static  Identity of(T value){
         return new Identity<>(value);
     }
    public static  Identity fromTuple(Tuple1 t1){
      return of(t1._1());
    }

     public T get(){
         return value;
     }
     public T extract(){
         return value;
     }
     public  R fold(Function fn){
         return fn.apply(value);
     }
     public Identity> nest(){
         return of(this);
     }
     public  Identity coflatMap(Function, ? extends R> fn){
         return of(fn.apply(this));
     }
    public  Identity map(Function fn){
         return new Identity<>(fn.apply(value));
     }
    public  Identity flatMap(Function> fn){
        return narrow(fn.apply(value));
    }

    public  Identity zip(Identity id,BiFunction fn){
      return flatMap(a->id.map(b->fn.apply(a,b)));
    }
    public Tuple1 toTuple(){
        return Tuple1.of(value);
    }

    public Tuple1 toLazyTuple(){
        return Tuple1.lazy(()->value);
    }



    public static  Identity narrow(Identity id){
        return (Identity)id;
    }
    public static  Identity narrowK(Higher ds){
        return (Identity)ds;
    }



    @Override
    public Iterator iterator() {
        return Arrays.asList(value).iterator();
    }

    @Override
    public Tuple1 unapply() {
        return Tuple.tuple(value);
    }

    public static  Higher widen(Identity narrow) {
      return narrow;
    }


    @Deprecated
  public static class Comprehensions {

    public static  Identity forEach(Identity id,
                                                                        Function> value2,
                                                                        Function, ? extends Identity> value3,
                                                                        Function, ? extends Identity> value4,
                                                                        Function, ? extends Identity> value5,
                                                                        Function, ? extends Identity> value6,
                                                                        Function, ? extends Identity> value7,
                                                                        Function, ? extends Identity> value8
    ) {

      return id.flatMap(in -> {

        Identity a = value2.apply(in);
        return a.flatMap(ina -> {
          Identity b = value3.apply(Tuple.tuple(in,ina));
          return b.flatMap(inb -> {

            Identity c = value4.apply(Tuple.tuple(in,ina,inb));

            return c.flatMap(inc->{
              Identity d = value5.apply(Tuple.tuple(in,ina,inb,inc));
              return d.flatMap(ind->{
                Identity e = value6.apply(Tuple.tuple(in,ina,inb,inc,ind));
                return e.flatMap(ine->{
                  Identity f = value7.apply(Tuple.tuple(in,ina,inb,inc,ind,ine));
                  return f.flatMap(inf->{
                    Identity g = value8.apply(Tuple.tuple(in,ina,inb,inc,ind,ine,inf));
                    return g;

                  });

                });
              });

            });

          });


        });


      });

    }
    public static  Identity forEach(Identity id,
                                                                     Function> value2,
                                                                     Function, ? extends Identity> value3,
                                                                     Function, ? extends Identity> value4,
                                                                     Function, ? extends Identity> value5,
                                                                     Function, ? extends Identity> value6,
                                                                     Function, ? extends Identity> value7
    ) {

      return id.flatMap(in -> {

        Identity a = value2.apply(in);
        return a.flatMap(ina -> {
          Identity b = value3.apply(Tuple.tuple(in,ina));
          return b.flatMap(inb -> {

            Identity c = value4.apply(Tuple.tuple(in,ina,inb));

            return c.flatMap(inc->{
              Identity d = value5.apply(Tuple.tuple(in,ina,inb,inc));
              return d.flatMap(ind->{
                Identity e = value6.apply(Tuple.tuple(in,ina,inb,inc,ind));
                return e.flatMap(ine->{
                  Identity f = value7.apply(Tuple.tuple(in,ina,inb,inc,ind,ine));
                  return f;
                });
              });

            });

          });


        });


      });

    }

    public static  Identity forEach(Identity id,
                                                                  Function> value2,
                                                                  Function, ? extends Identity> value3,
                                                                  Function, ? extends Identity> value4,
                                                                  Function, ? extends Identity> value5,
                                                                  Function, ? extends Identity> value6
    ) {

      return id.flatMap(in -> {

        Identity a = value2.apply(in);
        return a.flatMap(ina -> {
          Identity b = value3.apply(Tuple.tuple(in,ina));
          return b.flatMap(inb -> {

            Identity c = value4.apply(Tuple.tuple(in,ina,inb));

            return c.flatMap(inc->{
              Identity d = value5.apply(Tuple.tuple(in,ina,inb,inc));
              return d.flatMap(ind->{
                Identity e = value6.apply(Tuple.tuple(in,ina,inb,inc,ind));
                return e;
              });
            });

          });


        });


      });

    }
    public static  Identity forEach(Identity id,
                                                               Function> value2,
                                                               Function, ? extends Identity> value3,
                                                               Function, ? extends Identity> value4,
                                                               Function, ? extends Identity> value5

    ) {

      return id.flatMap(in -> {

        Identity a = value2.apply(in);
        return a.flatMap(ina -> {
          Identity b = value3.apply(Tuple.tuple(in,ina));
          return b.flatMap(inb -> {

            Identity c = value4.apply(Tuple.tuple(in,ina,inb));

            return c.flatMap(inc->{
              Identity d = value5.apply(Tuple.tuple(in,ina,inb,inc));
              return d;
            });

          });


        });


      });

    }
    public static  Identity forEach(Identity id,
                                                            Function> value2,
                                                            Function, ? extends Identity> value3,
                                                            Function, ? extends Identity> value4

    ) {

      return id.flatMap(in -> {

        Identity a = value2.apply(in);
        return a.flatMap(ina -> {
          Identity b = value3.apply(Tuple.tuple(in,ina));
          return b.flatMap(inb -> {

            Identity c = value4.apply(Tuple.tuple(in,ina,inb));

            return c;

          });


        });


      });

    }
    public static  Identity forEach(Identity id,
                                                        Function> value2,
                                                        Function, ? extends Identity> value3

    ) {

      return id.flatMap(in -> {

        Identity a = value2.apply(in);
        return a.flatMap(ina -> {
          Identity b = value3.apply(Tuple.tuple(in,ina));
          return b;


        });


      });

    }
    public static  Identity forEach(Identity id,
                                                    Function> value2


    ) {

      return id.flatMap(in -> {

        Identity a = value2.apply(in);
        return a;


      });

    }


  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy