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

com.aol.cyclops.vavr.hkt.VectorKind Maven / Gradle / Ivy

The newest version!
package com.aol.cyclops.vavr.hkt;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;


import com.aol.cyclops2.hkt.Higher;
import cyclops.collections.vavr.VavrListX;
import cyclops.collections.vavr.VavrVectorX;
import cyclops.companion.vavr.Lists;
import cyclops.companion.vavr.Vectors;
import cyclops.monads.VavrWitness;
import cyclops.monads.VavrWitness.vector;
import cyclops.monads.WitnessType;
import cyclops.monads.transformers.ListT;
import cyclops.typeclasses.Active;
import cyclops.typeclasses.InstanceDefinitions;
import cyclops.typeclasses.Nested;
import io.vavr.collection.Array;
import io.vavr.collection.Vector;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.Delegate;

/**
 * Simulates Higher Kinded Types for Vector's
 * 
 * VectorKind is a Vector and a Higher Kinded Type (vector,T)
 * 
 * @author johnmcclean
 *
 * @param  Data type stored within the Vector
 */
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public  class VectorKind implements Higher{

    public static  Higher widenK(final Vector completableList) {

        return new VectorKind<>(
                completableList);
    }
    public Active allTypeclasses(){
        return Active.of(this, Vectors.Instances.definitions());
    }

    public  Nested mapM(Function> fn, InstanceDefinitions defs){
        return Vectors.mapM(boxed,fn,defs);
    }
    public  VectorKind fold(Function,? extends Vector> op){
        return widen(op.apply(boxed));
    }
    public > ListT liftM(W witness) {
        return ListT.of(witness.adapter().unit(VavrVectorX.from(boxed)));
    }
    public static  VectorKind of(T element) {
        return  widen(Vector.of(element));
    }

   
    @SafeVarargs
    public static  VectorKind of(T... elements) {
        return widen(Vector.of(elements));
    }
    /**
     * Convert a Vector to a simulated HigherKindedType that captures Vector nature
     * and Vector element data type separately. Recover via @see VectorKind#narrow
     * 
     * If the supplied Vector implements VectorKind it is returned already, otherwise it
     * is wrapped into a Vector implementation that does implement VectorKind
     * 
     * @param list Vector to widen to a VectorKind
     * @return VectorKind encoding HKT info about Vectors
     */
    public static  VectorKind widen(final Vector list) {
        
        return new VectorKind<>(list);
    }
    /**
     * Widen a VectorKind nested inside another HKT encoded type
     * 
     * @param list HTK encoded type containing  a Vector to widen
     * @return HKT encoded type with a widened Vector
     */
    public static  Higher> widen2(Higher> list){
        //a functor could be used (if C2 is a functor / one exists for C2 type) instead of casting
        //cast seems safer as Higher must be a VectorKind
        return (Higher)list;
    }
    /**
     * Convert the raw Higher Kinded Type for Vector types into the VectorKind type definition class
     * 
     * @param list HKT encoded list into a VectorKind
     * @return VectorKind
     */
    public static  VectorKind narrowK(final Higher list) {
       return (VectorKind)list;
    }
    /**
     * Convert the HigherKindedType definition for a Vector into
     * 
     * @param list Type Constructor to convert back into narrowed type
     * @return VectorX from Higher Kinded Type
     */
    public static  Vector narrow(final Higher list) {
        return ((VectorKind)list).narrow();
       
    }


    @Delegate
    private final Vector boxed;

    /**
     * @return This back as a VectorX
     */
    public Vector narrow() {
        return (Vector) (boxed);
    }


    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return boxed.hashCode();
    }


    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        return boxed.equals(obj);
    }


    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "VectorKind [" + boxed + "]";
    }


    
    
      
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy