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

cyclops.collections.vavr.VavrVectorX Maven / Gradle / Ivy

package cyclops.collections.vavr;

import java.util.AbstractList;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

import com.aol.cyclops2.data.collections.extensions.CollectionX;
import com.aol.cyclops2.data.collections.extensions.lazy.immutable.LazyLinkedListX;
import com.aol.cyclops2.data.collections.extensions.lazy.immutable.LazyPVectorX;
import com.aol.cyclops2.types.Unwrapable;
import com.aol.cyclops2.types.foldable.Evaluation;
import cyclops.collections.immutable.VectorX;
import cyclops.function.Reducer;
import cyclops.stream.ReactiveSeq;
import io.vavr.collection.List;
import org.jooq.lambda.tuple.Tuple2;
import org.pcollections.PVector;


import io.vavr.collection.Vector;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.Wither;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class VavrVectorX extends AbstractList implements PVector, Unwrapable {

    public static  VectorX vectorX(ReactiveSeq stream){
        return fromStream(stream);
    }
    @Override
    public  R unwrap() {
        return (R)vector;
    }

    public static  VectorX copyFromCollection(CollectionX vec) {

        return VavrVectorX.empty()
                .plusAll(vec);

    }
    /**
     * Create a LazyPVectorX from a Stream
     * 
     * @param stream to construct a LazyQueueX from
     * @return LazyPVectorX
     */
    public static  LazyPVectorX fromStream(Stream stream) {
        return new LazyPVectorX(null, ReactiveSeq.fromStream(stream),toPVector(), Evaluation.LAZY);
    }

    /**
     * Create a LazyPVectorX that contains the Integers between start and end
     * 
     * @param start
     *            Number of range to start from
     * @param end
     *            Number for range to end at
     * @return Range ListX
     */
    public static LazyPVectorX range(int start, int end) {
        return fromStream(ReactiveSeq.range(start, end));
    }

    /**
     * Create a LazyPVectorX that contains the Longs between start and end
     * 
     * @param start
     *            Number of range to start from
     * @param end
     *            Number for range to end at
     * @return Range ListX
     */
    public static LazyPVectorX rangeLong(long start, long end) {
        return fromStream(ReactiveSeq.rangeLong(start, end));
    }

    /**
     * Unfold a function into a ListX
     * 
     * 
     * {@code 
     *  LazyPVectorX.unfold(1,i->i<=6 ? Optional.of(Tuple.tuple(i,i+1)) : Optional.empty());
     * 
     * //(1,2,3,4,5)
     * 
     * }
* * @param seed Initial value * @param unfolder Iteratively applied function, terminated by an empty Optional * @return ListX generated by unfolder function */ public static LazyPVectorX unfold(U seed, Function>> unfolder) { return fromStream(ReactiveSeq.unfold(seed, unfolder)); } /** * Generate a LazyPVectorX from the provided Supplier up to the provided limit number of times * * @param limit Max number of elements to generate * @param s Supplier to generate ListX elements * @return ListX generated from the provided Supplier */ public static LazyPVectorX generate(long limit, Supplier s) { return fromStream(ReactiveSeq.generate(s) .limit(limit)); } /** * Create a LazyPVectorX by iterative application of a function to an initial element up to the supplied limit number of times * * @param limit Max number of elements to generate * @param seed Initial element * @param f Iteratively applied to each element to generate the next element * @return ListX generated by iterative application */ public static LazyPVectorX iterate(long limit, final T seed, final UnaryOperator f) { return fromStream(ReactiveSeq.iterate(seed, f) .limit(limit)); } /** *
     * {@code 
     * PVector q = JSPVector.toPVector()
                                     .mapReduce(Stream.of(1,2,3,4));
     * 
     * }
     * 
* @return Reducer for PVector */ public static Reducer> toPVector() { return Reducer.> of(VavrVectorX.emptyPVector(), (final PVector a) -> b -> a.plusAll(b), (final T x) -> VavrVectorX.singleton(x)); } public static VavrVectorX emptyPVector(){ return new VavrVectorX<>(Vector.empty()); } public static LazyPVectorX empty(){ return fromPVector(new VavrVectorX<>(Vector.empty()), toPVector()); } private static LazyPVectorX fromPVector(PVector vec, Reducer> pVectorReducer) { return new LazyPVectorX(vec,null, pVectorReducer,Evaluation.LAZY); } public static LazyPVectorX singleton(T t){ return fromPVector(new VavrVectorX<>(Vector.of(t)), toPVector()); } public static LazyPVectorX of(T... t){ return fromPVector(new VavrVectorX<>(Vector.of(t)), toPVector()); } public static LazyPVectorX ofAll(Vector t){ return fromPVector(new VavrVectorX<>(t), toPVector()); } public static LazyPVectorX PVector(Vector q) { return fromPVector(new VavrVectorX(q), toPVector()); } public static LazyPVectorX from(Vector q) { return PVector(q); } @SafeVarargs public static LazyPVectorX PVector(T... elements){ return fromPVector(of(elements),toPVector()); } @Wither private final Vector vector; @Override public PVector plus(T e) { return withVector(vector.append(e)); } @Override public PVector plusAll(Collection list) { return withVector(vector.appendAll(list)); } @Override public PVector with(int i, T e) { return withVector(vector.insert(i,e)); } @Override public PVector plus(int i, T e) { return withVector(vector.insert(i,e)); } @Override public PVector plusAll(int i, Collection list) { return withVector(vector.insertAll(i,list)); } @Override public PVector minus(Object e) { return withVector(vector.remove((T)e)); } @Override public PVector minusAll(Collection list) { return withVector(vector.removeAll((Collection)list)); } @Override public PVector minus(int i) { return withVector(vector.removeAt(i)); } @Override public PVector subList(int start, int end) { return withVector(vector.subSequence(start, end)); } @Override public T get(int index) { return vector.get(index); } @Override public int size() { return vector.size(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy