public Vector<T> dropRight(int num){ if(num<=0) return this; if(num>=size()) return empty(); if(tail.size()==1){ 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.min(tail.size(),num))).dropRight(num-tail.size()); } return unitStream(stream().dropRight(num)); } @Override
@Test @Ignore public void test5Pow(){ Vector<Integer> ints = Vector.<Integer>empty(); int p = Double.valueOf(Math.pow(2,25)).intValue(); for(int i=0;i<p;i++){ ints = ints.plus(i); } for(int i=0;i<p;i++){ assertThat(ints.get(i),equalTo(Option.some(i))); } final Vector<Integer> finalRef = ints; ints.stream().forEach(next-> { assertThat(finalRef.get(next), equalTo(Option.some(next))); } ); Vector<Integer> doubled = ints.map(n->n*2); Iterator<Integer> it = doubled.iterator(); for(int i=0;i<p;i++){ Integer next = it.next(); assertThat(next, equalTo(i * 2)); } }
@Test @Ignore public void test4PowSet(){ Vector<Integer> ints = Vector.<Integer>empty(); int p = Double.valueOf(Math.pow(2,20)).intValue(); for(int i=0;i<p;i++){ ints = ints.plus(i); } for(int i=0;i<p;i++){ ints = ints.updateAt(i,i*2); } for(int i=0;i<p;i++){ assertThat(ints.get(i),equalTo(Option.some(i*2))); } Vector<Integer> doubled = ints.map(n->n*2); Iterator<Integer> it = doubled.iterator(); for(int i=0;i<p;i++){ Integer next = it.next(); assertThat(next, equalTo(i * 4)); } } @Test @Ignore
@Test public void test3PowSet(){ Vector<Integer> ints = Vector.<Integer>empty(); int p = Double.valueOf(Math.pow(2,15)).intValue(); for(int i=0;i<p;i++){ ints = ints.plus(i); } for(int i=0;i<p;i++){ System.out.println(i); ints = ints.updateAt(i,i*2); } for(int i=0;i<p;i++){ assertThat(ints.get(i),equalTo(Option.some(i*2))); } } @Test @Ignore
@Test public void testCollectionConcatPVector() { cyclops.data.Vector<Integer> list = cyclops.data.Vector.empty(); list= list.plus(1); list = list.plus(2); list = list.plus(4); Semigroup<Vector<Integer>> combiner= Semigroups.vectorConcat(); assertThat(combiner.apply(list,Vector.of(4,5,6)).toList(),equalTo(Arrays.asList(1,2,4,4,5,6))); } @Test
@Test public void testTraverseLeft1() { Vector<LazyEither5<Integer,String,String,String,String>> list = Vector.of(just,none, LazyEither5.<String,String,String,String,Integer>right(1)).map(LazyEither5::swap1); LazyEither5<Integer, String, String, String, ReactiveSeq<String>> xors = LazyEither5.traverse(list, s -> "hello:" + s); assertThat(xors.map(s->s.toList()),equalTo(LazyEither5.right(Arrays.asList("hello:none")))); } @Test
public static <T,R> Vector<R> tailRec(T initial, Function<? super T, ? extends Vector<? extends Either<T, R>>> fn) { Vector<Either<T, R>> next = Vector.of(Either.left(initial)); boolean newValue[] = {true}; for(;;){ next = next.flatMap(e -> e.fold(s -> { newValue[0]=true; return fn.apply(s); }, p -> { newValue[0]=false; return Vector.of(e); })); if(!newValue[0]) break; } return Vector.fromStream(Either.sequenceRight(next).orElse(ReactiveSeq.empty())); } public static <T> Higher<vector, T> widen(Vector<T> narrow) {
@Test @Ignore public void test4Pow(){ Vector<Integer> ints = Vector.<Integer>empty(); int p = Double.valueOf(Math.pow(2,20)).intValue(); for(int i=0;i<p;i++){ ints = ints.plus(i); } for(int i=0;i<p;i++){ assertThat(ints.get(i),equalTo(Option.some(i))); } final Vector<Integer> finalRef = ints; ints.stream().forEach(next-> { assertThat(finalRef.get(next), equalTo(Option.some(next))); } ); }
@Test public void raceError(){ AtomicReference<Vector<Integer>> data = new AtomicReference(Vector.empty()); AtomicBoolean complete = new AtomicBoolean(false); AtomicReference<Throwable> error = new AtomicReference<Throwable>(null); Spouts.amb(Future.ofError(new RuntimeException()), Future.<Integer>future()).forEach(z->{ assertFalse(complete.get()); data.updateAndGet(s->s.plus(z)); },e->{ error.set(e); },()->{ complete.set(true); }); assertThat("Values " + data,data.get().size(), equalTo(0)); Assert.assertThat(complete.get(), IsEqual.equalTo(true)); assertThat(error.get(),instanceOf(RuntimeException.class)); }