cyclops.collections.vavr.VavrListX 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 cyclops.collections.vavr;
import java.util.AbstractList;
import java.util.Collection;
import java.util.Iterator;
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.FoldToList;
import com.aol.cyclops2.data.collections.extensions.lazy.immutable.LazyLinkedListX;
import com.aol.cyclops2.types.Unwrapable;
import com.aol.cyclops2.types.foldable.Evaluation;
import cyclops.collections.immutable.LinkedListX;
import cyclops.collections.immutable.PersistentQueueX;
import cyclops.function.Reducer;
import cyclops.stream.ReactiveSeq;
import org.jooq.lambda.tuple.Tuple2;
import org.pcollections.PStack;
import io.vavr.collection.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.Wither;
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class VavrListX extends AbstractList implements PStack, Unwrapable {
public static LinkedListX listX(ReactiveSeq stream){
return fromStream(stream);
}
@Override
public R unwrap() {
return (R)list;
}
static final FoldToList gen = (it,i)-> VavrListX.from(from(it,i));
public static LinkedListX copyFromCollection(CollectionX vec) {
List list = from(vec.iterator(),0);
return from(list);
}
private static List from(final Iterator i, int depth) {
if(!i.hasNext())
return List.empty();
E e = i.next();
return from(i,depth++).prepend(e);
}
/**
* Create a LazyLinkedListX from a Stream
*
* @param stream to construct a LazyQueueX from
* @return LazyLinkedListX
*/
public static LazyLinkedListX fromStream(Stream stream) {
Reducer> p = toPStack();
return new LazyLinkedListX(null, ReactiveSeq.fromStream(stream),p, gen,Evaluation.LAZY);
}
/**
* Create a LazyLinkedListX 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 LazyLinkedListX range(int start, int end) {
return fromStream(ReactiveSeq.range(start, end));
}
/**
* Create a LazyLinkedListX 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 LazyLinkedListX rangeLong(long start, long end) {
return fromStream(ReactiveSeq.rangeLong(start, end));
}
/**
* Unfold a function into a ListX
*
*
* {@code
* LazyLinkedListX.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 LazyLinkedListX unfold(U seed, Function super U, Optional>> unfolder) {
return fromStream(ReactiveSeq.unfold(seed, unfolder));
}
/**
* Generate a LazyLinkedListX 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 LazyLinkedListX generate(long limit, Supplier s) {
return fromStream(ReactiveSeq.generate(s)
.limit(limit));
}
/**
* Create a LazyLinkedListX 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 LazyLinkedListX iterate(long limit, final T seed, final UnaryOperator f) {
return fromStream(ReactiveSeq.iterate(seed, f)
.limit(limit));
}
/**
*
* {@code
* PVector q = JSPStack.toPStack()
.mapReduce(Stream.of(1,2,3,4));
*
* }
*
* @return Reducer for PVector
*/
public static Reducer> toPStack() {
return Reducer.> of(VavrListX.emptyPStack(), (final PStack a) -> b -> a.plusAll(b), (final T x) -> VavrListX.singleton(x));
}
public static VavrListX emptyPStack(){
return new VavrListX(List.empty());
}
public static LazyLinkedListX empty(){
return fromPStack(new VavrListX(List.empty()), toPStack());
}
private static LazyLinkedListX fromPStack(PStack s, Reducer> pStackReducer) {
return new LazyLinkedListX(s,null, pStackReducer, gen, Evaluation.LAZY);
}
public static LazyLinkedListX singleton(T t){
return fromPStack(new VavrListX(List.of(t)), toPStack());
}
public static LazyLinkedListX of(T... t){
return fromPStack(new VavrListX(List.of(t)), toPStack());
}
public static LazyLinkedListX ofAll(List q){
return fromPStack(new VavrListX(q), toPStack());
}
public static LazyLinkedListX PStack(List q) {
return fromPStack(new VavrListX<>(q), toPStack());
}
public static LazyLinkedListX from(List q) {
return fromPStack(new VavrListX<>(q), toPStack());
}
@SafeVarargs
public static LazyLinkedListX PStack(T... elements){
return fromPStack(of(elements),toPStack());
}
@Wither
private final List list;
@Override
public PStack plus(T e) {
return withList(list.prepend(e));
}
@Override
public PStack plusAll(Collection extends T> l) {
List use = list;
for(T next : l)
use = use.prepend(next);
return withList(use);
}
@Override
public PStack with(int i, T e) {
List front = list.take(i);
List back = list.drop(i);
return withList(back.prepend(e).prependAll(front));
}
@Override
public PStack plus(int i, T e) {
return withList(list.insert(i,e));
}
@Override
public PStack plusAll(int i, Collection extends T> l) {
//use same behaviour as pCollections
List use = list;
for(T next : l)
use = use.insert(i,next);
return withList(use);
}
@Override
public PStack minus(Object e) {
return withList(list.remove((T)e));
}
@Override
public PStack minusAll(Collection> l) {
return withList(list.removeAll((Collection)l));
}
@Override
public PStack minus(int i) {
return withList(list.removeAt(i));
}
@Override
public PStack subList(int start, int end) {
return withList(list.subSequence(start, end));
}
@Override
public T get(int index) {
return list.get(index);
}
@Override
public int size() {
return list.size();
}
@Override
public org.pcollections.PStack subList(int start) {
return withList(list.subSequence(start));
}
}