com.aol.cyclops.vavr.hkt.ArrayKind Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops-vavr Show documentation
Show all versions of cyclops-vavr Show documentation
Converters and Comprehenders for Javaslang
The newest version!
package com.aol.cyclops.vavr.hkt;
import com.aol.cyclops2.hkt.Higher;
import com.aol.cyclops2.types.Unwrapable;
import cyclops.companion.vavr.Arrays;
import cyclops.companion.vavr.Lazys;
import cyclops.monads.VavrWitness;
import cyclops.monads.VavrWitness.array;
import cyclops.typeclasses.Active;
import cyclops.typeclasses.InstanceDefinitions;
import cyclops.typeclasses.Nested;
import io.vavr.Lazy;
import io.vavr.collection.Array;
import io.vavr.collection.List;
import io.vavr.collection.Queue;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.Delegate;
import java.util.function.Function;
/**
* Simulates Higher Kinded Types for Array's
*
* ArrayKind is a Array and a Higher Kinded Type (array,T)
*
* @author johnmcclean
*
* @param Data type stored within the Array
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ArrayKind implements Higher{
public static Higher widenK(final Array completableList) {
return new ArrayKind<>(
completableList);
}
public Active allTypeclasses(){
return Active.of(this, Arrays.Instances.definitions());
}
public Nested mapM(Function super T,? extends Higher> fn, InstanceDefinitions defs){
Array> e = map(fn);
ArrayKind> lk = ArrayKind.widen(e);
return Nested.of(lk, Arrays.Instances.definitions(), defs);
}
public ArrayKind fold(Function super Array super T>,? extends Array> op){
return widen(op.apply(boxed));
}
public static ArrayKind of(T element) {
return widen(Array.of(element));
}
@SafeVarargs
public static ArrayKind of(T... elements) {
return widen(Array.of(elements));
}
/**
* Convert a Array to a simulated HigherKindedType that captures Array nature
* and Array element data type separately. Recover via @see ArrayKind#narrow
*
* If the supplied Array implements ArrayKind it is returned already, otherwise it
* is wrapped into a Array implementation that does implement ArrayKind
*
* @param list Array to widen to a ArrayKind
* @return ArrayKind encoding HKT info about Arrays
*/
public static ArrayKind widen(final Array list) {
return new ArrayKind<>(list);
}
/**
* Widen a ArrayKind nested inside another HKT encoded type
*
* @param list HTK encoded type containing a Array to widen
* @return HKT encoded type with a widened Array
*/
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 ArrayKind
return (Higher)list;
}
/**
* Convert the raw Higher Kinded Type for Array types into the ArrayKind type definition class
*
* @param list HKT encoded list into a ArrayKind
* @return ArrayKind
*/
public static ArrayKind narrowK(final Higher list) {
return (ArrayKind)list;
}
/**
* Convert the HigherKindedType definition for a Array into
*
* @param list Type Constructor to convert back into narrowed type
* @return ArrayX from Higher Kinded Type
*/
public static Array narrow(final Higher list) {
return ((ArrayKind)list).narrow();
}
@Delegate
private final Array boxed;
/**
* @return This back as a ArrayX
*/
public Array narrow() {
return (Array) (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 "ArrayKind [" + boxed + "]";
}
}