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

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

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

import com.aol.cyclops2.hkt.Higher;
import cyclops.collections.vavr.VavrVectorX;
import cyclops.companion.vavr.Streams;
import cyclops.companion.vavr.Vectors;
import cyclops.monads.VavrWitness;
import cyclops.monads.VavrWitness.stream;
import cyclops.monads.WitnessType;
import cyclops.monads.transformers.ListT;
import cyclops.monads.transformers.StreamT;
import cyclops.stream.ReactiveSeq;
import cyclops.typeclasses.Active;
import cyclops.typeclasses.InstanceDefinitions;
import cyclops.typeclasses.Nested;
import io.vavr.collection.Array;
import io.vavr.collection.Vector;
import io.vavr.concurrent.Future;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;


import io.vavr.collection.Stream;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

import java.util.function.Function;

/**
 * Simulates Higher Kinded Types for Vavr Stream's
 * 
 * StreamKind is a Stream and a Higher Kinded Type (StreamKind.µ,T)
 * 
 * @author johnmcclean
 *
 * @param  Data type stored within the Stream
 */

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public final class StreamKind implements Higher, Publisher, Stream{

    public Active allTypeclasses(){
        return Active.of(this, Streams.Instances.definitions());
    }

    public static  Higher widenK(final Stream completableList) {

        return new StreamKind<>(
                completableList);
    }
    public  Nested mapM(Function> fn, InstanceDefinitions defs){
        return Streams.mapM(boxed,fn,defs);
    }

    public > StreamT liftM(W witness) {
        return StreamT.of(witness.adapter().unit(ReactiveSeq.fromStream(boxed.toJavaStream())));
    }
    public  StreamKind fold(Function,? extends Stream> op){
        return widen(op.apply(boxed));
    }
    /**
     * Construct a HKT encoded completed Stream
     * 
     * @param value To encode inside a HKT encoded Stream
     * @return Completed HKT encoded FStream
     */
    public static  StreamKind just(T value) {

        return widen(Stream.of(value));
    }

    public static  StreamKind just(T... values) {

        return widen(Stream.of(values));
    }

    public static  StreamKind empty() {
        return widen(Stream.empty());
    }

    /**
     * Convert a Stream to a simulated HigherKindedType that captures Stream nature
     * and Stream element data type separately. Recover via @see StreamKind#narrow
     * 
     * If the supplied Stream implements StreamKind it is returned already, otherwise it
     * is wrapped into a Stream implementation that does implement StreamKind
     * 
     * @param stream Stream to widen to a StreamKind
     * @return StreamKind encoding HKT info about Streams
     */
    public static  StreamKind widen(final Stream stream) {

        return new StreamKind<>(stream);
    }

    /**
     * Widen a StreamKind nested inside another HKT encoded type
     * 
     * @param flux HTK encoded type containing  a Stream to widen
     * @return HKT encoded type with a widened Stream
     */
    public static  Higher> widen2(Higher> flux) {
        // 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 StreamKind
        return (Higher) flux;
    }

    public static  StreamKind widen(final Publisher completableStream) {

        return new StreamKind<>(
                                Stream.ofAll((java.util.stream.Stream)ReactiveSeq.fromPublisher(completableStream)));
    }

    /**
     * Convert the raw Higher Kinded Type for StreamKind types into the StreamKind type definition class
     * 
     * @param future HKT encoded list into a StreamKind
     * @return StreamKind
     */
    public static  StreamKind narrowK(final Higher future) {
        return (StreamKind) future;
    }

    /**
     * Convert the HigherKindedType definition for a Stream into
     * 
     * @param stream Type Constructor to convert back into narrowed type
     * @return Stream from Higher Kinded Type
     */
    public static  Stream narrow(final Higher stream) {

        return ((StreamKind) stream).narrow();

    }

    private final Stream boxed;

    public ReactiveSeq toReactiveSeq(){
        return ReactiveSeq.fromIterable(boxed);
    }
    /**
     * @return wrapped Stream
     */
    public Stream narrow() {
        return boxed;
    }

    @Override
    public void subscribe(Subscriber s) {
        ReactiveSeq.fromIterable(boxed)
                   .subscribe(s);

    }

   

    
    public T head() {
        return boxed.head();
    }
    

    /**
     * @param o
     * @return
     * @see io.vavr.Value#equals(java.lang.Object)
     */
    public boolean equals(Object o) {
        return boxed.equals(o);
    }

    

    /**
     * @return
     * @see io.vavr.Value#hashCode()
     */
    public int hashCode() {
        return boxed.hashCode();
    }

   
    /**
     * @return
     * @see io.vavr.Value#toString()
     */
    public String toString() {
        return boxed.toString();
    }

   
    

    /**
     * @return
     * @see io.vavr.collection.Stream#tail()
     */
    public Stream tail() {
        return boxed.tail();
    }

    /**
     * @return
     * @see io.vavr.collection.Traversable#isEmpty()
     */
    public boolean isEmpty() {
        return boxed.isEmpty();
    }

    

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy