Skip to content

Commit

Permalink
Merge pull request #895 from aol/vectorDropFix
Browse files Browse the repository at this point in the history
fix for #894
  • Loading branch information
johnmcclean authored Jul 24, 2018
2 parents 2b36583 + 69677ab commit d06a9b4
Show file tree
Hide file tree
Showing 27 changed files with 230 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static <T> LinkedListX<T> defer(Supplier<LinkedListX<T>> s){
}

default Tuple2<LinkedListX<T>, LinkedListX<T>> splitAt(int n) {
materialize();
return tuple(take(n), drop(n));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ default OrderedSetX<T> materialize() {
}

default Tuple2<OrderedSetX<T>, OrderedSetX<T>> splitAt(int n) {
materialize();
return Tuple.tuple(take(n), drop(n));
}
default Tuple2<OrderedSetX<T>, OrderedSetX<T>> span(Predicate<? super T> pred) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public static <T> PersistentQueueX<T> fromIterable(final Iterable<T> iterable) {
}

default Tuple2<PersistentQueueX<T>, PersistentQueueX<T>> splitAt(int n) {
materialize();
return Tuple.tuple(take(n), drop(n));
}
default Tuple2<PersistentQueueX<T>, PersistentQueueX<T>> span(Predicate<? super T> pred) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ public static <T> VectorX<T> fromIterable(final Iterable<T> iterable) {
}
VectorX<T> type(Reducer<? extends PersistentList<T>,T> reducer);
default Tuple2<VectorX<T>, VectorX<T>> splitAt(int n) {
materialize();
return Tuple.tuple(take(n), drop(n));
}
default Tuple2<VectorX<T>, VectorX<T>> span(Predicate<? super T> pred) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public interface DequeX<T> extends To<DequeX<T>>,
DequeX<T> lazy();
DequeX<T> eager();
default Tuple2<DequeX<T>, DequeX<T>> splitAt(int n) {
materialize();
return Tuple.tuple(take(n), drop(n));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public interface ListX<T> extends To<ListX<T>>,


default Tuple2<ListX<T>, ListX<T>> splitAt(int n) {
materialize();
return Tuple.tuple(take(n), drop(n));
}
default Tuple2<ListX<T>, ListX<T>> span(Predicate<? super T> pred) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static <T> QueueX<T> defer(Supplier<QueueX<T>> s){
.concatMap(l->l);
}
default Tuple2<QueueX<T>, QueueX<T>> splitAt(int n) {
materialize();
return Tuple.tuple(take(n), drop(n));
}
default Tuple2<QueueX<T>, QueueX<T>> span(Predicate<? super T> pred) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static <T> SortedSetX<T> defer(Supplier<SortedSetX<T>> s){
.concatMap(l->l);
}
default Tuple2<SortedSetX<T>, SortedSetX<T>> splitAt(int n) {
materialize();
return Tuple.tuple(take(n), drop(n));
}
default Tuple2<SortedSetX<T>, SortedSetX<T>> span(Predicate<? super T> pred) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void testPartition() {
assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> true)._2().toList());

assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> false)._1().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._2().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._1().toList());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testPartition() {
assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> true)._2().toList());

assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> false)._1().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._2().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._1().toList());
}
@Test
public void asyncTest() throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testPartition() {
assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> true)._2().toList());

assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> false)._1().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._2().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._1().toList());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testPartition() {
assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> true)._2().toList());

assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> false)._1().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._2().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._1().toList());
}
@Test
public void asyncTest() throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testPartition() {
assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> true)._2().toList());

assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> false)._1().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._2().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._1().toList());
}
@Test
public void asyncTest() throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public void splitBy(){
assertThat(of(1,2,3).splitBy(i->i<0),equalTo(Tuple.tuple(of(1,2,3),of())));
}

@Test
public void takeDrop(){
assertThat(of(1,2,3).take(2),equalTo(of(1,2)));
assertThat(of(1,2,3).drop(2),equalTo(of(3)));
}
@Test
public void splitAtTest(){
assertThat(of(1,2,3).splitAt(4) ,equalTo(Tuple.tuple(of(1,2,3),of())));
Expand Down Expand Up @@ -85,7 +90,7 @@ public void testPartition() {
assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> true)._2().toList());

assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> false)._1().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._2().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._1().toList());
}
@Test
public void maybe() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testPartition() {
assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> true)._2().toList());

assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> false)._1().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._2().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._1().toList());
}
@Test
public void asyncTest() throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testPartition() {
assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> true)._2().toList());

assertEquals(asList(), of(1, 2, 3, 4, 5, 6).partition(i -> false)._1().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._2().toList());
assertEquals(asList(1, 2, 3, 4, 5, 6), of(1, 2, 3, 4, 5, 6).splitBy(i -> false)._1().toList());
}
@Test
public void emptyAllCombinations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,16 +836,17 @@ public <C extends PersistentCollection<? super T>> ReactiveSeq<C> grouped(final

@Override
public ReactiveSeq<T> skipLast(final int num) {
if (num == 1)

if(num==1)
return createSeq(new SkipLastOneOperator<>(source));
return createSeq(new SkipLastOperator<>(source, num));
return createSeq(new SkipLastOperator<>(source, num < 0 ? 0 : num));
}

@Override
public ReactiveSeq<T> limitLast(final int num) {
if (num == 1)
return createSeq(new LimitLastOneOperator<>(source));
return createSeq(new LimitLastOperator<>(source, num));
return createSeq(new LimitLastOperator<>(source, num < 0 ? 0 : num));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,12 +675,12 @@ public <C extends PersistentCollection<? super T>> ReactiveSeq<C> grouped(final

@Override
public ReactiveSeq<T> skipLast(final int num) {
return createSeq(SkipLastSpliterator.skipLast(get(), num), this.reversible);
return createSeq(SkipLastSpliterator.skipLast(get(), num < 0 ? 0 : num), this.reversible);
}

@Override
public ReactiveSeq<T> limitLast(final int num) {
return createSeq(LimitLastSpliterator.limitLast(get(), num), this.reversible);
return createSeq(LimitLastSpliterator.limitLast(get(), num < 0 ? 0 : num), this.reversible);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion cyclops/src/main/java/cyclops/data/LazySeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ public T foldRight(Monoid<T> reducer) {
public <R> R lazyFoldRight(R zero, BiFunction<? super T,Supplier<R>, ? extends R> f){


long inc = 800;
long inc = 500;
LazySeq<T> host = this;
long count = inc;
LazySeq<T> next = host.limit(count);
Expand Down
11 changes: 6 additions & 5 deletions cyclops/src/main/java/cyclops/data/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ public Vector<T> dropRight(int num){
return new Vector<>(this.root,BAMT.ActiveTail.emptyTail(),size()-1).drop(num-1);
}
if(tail.size()>0){
return new Vector<>(this.root,tail.dropRight(num),size()-(Math.max(tail.size(),num))).dropRight(num-tail.size());
return new Vector<>(this.root,tail.dropRight(num),size()-(Math.min(tail.size(),num))).dropRight(num-tail.size());
}
return unitStream(stream().dropRight(num));
}
Expand All @@ -725,7 +725,7 @@ public Vector<T> drop(long num) {
if(num>=size())
return empty();
if(size()<32){
return new Vector<>(this.root,tail.drop((int)num),size()-1);
return new Vector<>(this.root,tail.drop((int)num),size()-(int)num);
}
return unitStream(stream().drop(num));
}
Expand Down Expand Up @@ -783,7 +783,7 @@ public Option<T> get(int pos){
if(pos>=tailStart){
return tail.get(pos-tailStart);
}
return ((BAMT.PopulatedArray<T>)root).get(pos);
return root.get(pos);

}

Expand All @@ -796,7 +796,8 @@ public T getOrElse(int pos, T alt) {
if(pos>=tailStart){
return tail.getOrElse(pos-tailStart,alt);
}
return ((BAMT.PopulatedArray<T>)root).getOrElse(pos,alt);

return root.getOrElse(pos,alt);
}

@Override
Expand All @@ -808,7 +809,7 @@ public T getOrElseGet(int pos, Supplier<? extends T> alt) {
if(pos>=tailStart){
return tail.getOrElse(pos-tailStart,alt.get());
}
return ((BAMT.PopulatedArray<T>)root).getOrElse(pos,alt.get());
return root.getOrElse(pos,alt.get());
}

class VectorSome extends Vector<T> implements ImmutableList.Some<T>{
Expand Down
12 changes: 12 additions & 0 deletions cyclops/src/main/java/cyclops/data/base/BAMT.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ default <R> R match(Function<? super Zero<T>, ? extends R> zeroFn, Function<? s
}

ReactiveSeq<T> stream();
public T getOrElseGet(int pos, Supplier<T> alt);
public T getOrElse(int pos, T alt);
public Option<T> get(int pos);

}
public interface PopulatedArray<T> extends NestedArray<T>{
Expand Down Expand Up @@ -165,6 +168,15 @@ public NestedArray<T> append(ActiveTail<T> tail) {
public ReactiveSeq<T> stream() {
return ReactiveSeq.empty();
}
public T getOrElseGet(int pos, Supplier<T> alt){
return alt.get();
}
public T getOrElse(int pos, T alt){
return alt;
}
public Option<T> get(int pos){
return Option.none();
}
}

@AllArgsConstructor
Expand Down
23 changes: 23 additions & 0 deletions cyclops/src/test/java/cyclops/data/VectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cyclops.control.Maybe;
import cyclops.data.base.BAMT;
import cyclops.data.tuple.Tuple;
import cyclops.data.tuple.Tuple2;
import cyclops.control.Option;
import cyclops.data.basetests.BaseImmutableListTest;
Expand All @@ -27,6 +28,28 @@ protected <T> Vector<T> fromStream(Stream<T> s) {
return Vector.fromStream(s);
}

@Test
public void dropSize(){
System.out.println(of(1,2,3).drop(2));
System.out.println(of(1,2,3).drop(2).size());
assertThat(of(1,2,3).drop(2).size(),equalTo(1));
}
@Test
public void types(){
System.out.println(of(1,2,3).splitAt(2)._1().getClass());
System.out.println(of(1,2,3).splitAt(2)._1());
System.out.println(of(1,2,3).splitAt(2)._2().getClass());
System.out.println(of(1,2,3).splitAt(2)._2());

System.out.println(Tuple.tuple(of(1,2),of(3))._1().getClass());
System.out.println(Tuple.tuple(of(1,2),of(3))._1());
System.out.println(Tuple.tuple(of(1,2),of(3))._2().getClass());
assertThat(of(1,2,3).splitAt(2)._1() ,equalTo(of(1,2)));
ImmutableList<Integer> three = of(1,2,3).splitAt(2)._2();
three.equals(of(3));
assertThat(of(1,2,3).splitAt(2)._2() ,equalTo(of(3)));
assertThat(of(1,2,3).splitAt(2) ,equalTo(Tuple.tuple(of(1,2),of(3))));
}
@Test
public void equalsAndHash(){
LinkedList<Integer> l = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,47 @@ public abstract class BaseImmutableListTest extends AbstractIterableXTest {
public abstract <T> ImmutableList<T> of(T... values);
public abstract <T> ImmutableList<T> empty();

@Test
public void appendAllMultiple(){
assertThat(of(1,2,3).appendAll(of()),equalTo(of(1,2,3)));
assertThat(of(1,2,3).appendAll(of(4,5)),equalTo(of(1,2,3,4,5)));
}
@Test
public void takeValues(){
assertThat(of(1,2,3).take(-1),equalTo(of()));
assertThat(of(1,2,3).take(0),equalTo(of()));
assertThat(of(1,2,3).take(1),equalTo(of(1)));
assertThat(of(1,2,3).take(2),equalTo(of(1,2)));
assertThat(of(1,2,3).take(3),equalTo(of(1,2,3)));
assertThat(of(1,2,3).take(4),equalTo(of(1,2,3)));
}
@Test
public void takeRightValues(){
assertThat(of(1,2,3).takeRight(-1),equalTo(of()));
assertThat(of(1,2,3).takeRight(0),equalTo(of()));
assertThat(of(1,2,3).takeRight(1),equalTo(of(3)));
assertThat(of(1,2,3).takeRight(2),equalTo(of(2,3)));
assertThat(of(1,2,3).takeRight(3),equalTo(of(1,2,3)));
assertThat(of(1,2,3).takeRight(4),equalTo(of(1,2,3)));
}
@Test
public void dropValues(){
assertThat(of(1,2,3).drop(-1),equalTo(of(1,2,3)));
assertThat(of(1,2,3).drop(0),equalTo(of(1,2,3)));
assertThat(of(1,2,3).drop(1),equalTo(of(2,3)));
assertThat(of(1,2,3).drop(2),equalTo(of(3)));
assertThat(of(1,2,3).drop(3),equalTo(of()));
assertThat(of(1,2,3).drop(4),equalTo(of()));
}
@Test
public void dropRightValues(){
assertThat(of(1,2,3).dropRight(-1),equalTo(of(1,2,3)));
assertThat(of(1,2,3).dropRight(0),equalTo(of(1,2,3)));
assertThat(of(1,2,3).dropRight(1),equalTo(of(1,2)));
assertThat(of(1,2,3).dropRight(2),equalTo(of(1)));
assertThat(of(1,2,3).dropRight(3),equalTo(of()));
assertThat(of(1,2,3).dropRight(4),equalTo(of()));
}
@Test
public void span(){

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,47 @@ public abstract class BaseImmutableQueueTest extends AbstractIterableXTest {
@Override
public abstract <T> ImmutableQueue<T> of(T... values);
@Test
public void appendAllMultiple(){
assertThat(of(1,2,3).appendAll(of()),equalTo(of(1,2,3)));
assertThat(of(1,2,3).appendAll(of(4,5)),equalTo(of(1,2,3,4,5)));
}
@Test
public void takeValues(){
assertThat(of(1,2,3).take(-1),equalTo(of()));
assertThat(of(1,2,3).take(0),equalTo(of()));
assertThat(of(1,2,3).take(1),equalTo(of(1)));
assertThat(of(1,2,3).take(2),equalTo(of(1,2)));
assertThat(of(1,2,3).take(3),equalTo(of(1,2,3)));
assertThat(of(1,2,3).take(4),equalTo(of(1,2,3)));
}
@Test
public void takeRightValues(){
assertThat(of(1,2,3).takeRight(-1),equalTo(of()));
assertThat(of(1,2,3).takeRight(0),equalTo(of()));
assertThat(of(1,2,3).takeRight(1),equalTo(of(3)));
assertThat(of(1,2,3).takeRight(2),equalTo(of(2,3)));
assertThat(of(1,2,3).takeRight(3),equalTo(of(1,2,3)));
assertThat(of(1,2,3).takeRight(4),equalTo(of(1,2,3)));
}
@Test
public void dropValues(){
assertThat(of(1,2,3).drop(-1),equalTo(of(1,2,3)));
assertThat(of(1,2,3).drop(0),equalTo(of(1,2,3)));
assertThat(of(1,2,3).drop(1),equalTo(of(2,3)));
assertThat(of(1,2,3).drop(2),equalTo(of(3)));
assertThat(of(1,2,3).drop(3),equalTo(of()));
assertThat(of(1,2,3).drop(4),equalTo(of()));
}
@Test
public void dropRightValues(){
assertThat(of(1,2,3).dropRight(-1),equalTo(of(1,2,3)));
assertThat(of(1,2,3).dropRight(0),equalTo(of(1,2,3)));
assertThat(of(1,2,3).dropRight(1),equalTo(of(1,2)));
assertThat(of(1,2,3).dropRight(2),equalTo(of(1)));
assertThat(of(1,2,3).dropRight(3),equalTo(of()));
assertThat(of(1,2,3).dropRight(4),equalTo(of()));
}
@Test
public void span(){

assertThat(of(1,2,3,4,1,2,3,4).span(i->i<3),equalTo(Tuple.tuple(of(1,2),of(3,4,1,2,3,4))));
Expand Down
Loading

0 comments on commit d06a9b4

Please sign in to comment.