Skip to content

Commit

Permalink
Stackbuste test:
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmcclean committed Aug 8, 2017
1 parent 2200285 commit 50b2bfa
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 26 deletions.
67 changes: 41 additions & 26 deletions cyclops-vavr/src/main/java/cyclops/companion/vavr/Streams.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cyclops.companion.vavr;

import cyclops.control.*;
import cyclops.monads.VavrWitness.queue;
import cyclops.monads.VavrWitness.tryType;
import io.vavr.Lazy;
Expand All @@ -9,10 +10,6 @@
import com.aol.cyclops.vavr.hkt.*;
import cyclops.companion.CompletableFutures;
import cyclops.companion.Optionals;
import cyclops.control.Eval;
import cyclops.control.Maybe;
import cyclops.control.Reader;
import cyclops.control.Xor;
import cyclops.conversion.vavr.FromCyclopsReact;
import cyclops.monads.*;
import cyclops.monads.VavrWitness.*;
Expand All @@ -39,6 +36,7 @@
import cyclops.typeclasses.monad.*;
import io.vavr.collection.List;
import io.vavr.collection.Stream;
import io.vavr.control.Try;
import lombok.experimental.UtilityClass;
import org.jooq.lambda.tuple.Tuple2;

Expand Down Expand Up @@ -72,13 +70,20 @@ public static <T,R> Stream<R> tailRec(T initial, Function<? super T, ? extends
boolean newValue[] = {true};
for(;;){

next = next.flatMap(e -> e.fold(s -> {
newValue[0]=true;
return fn.apply(s); },
p -> {
newValue[0]=false;
return Stream.of(e);
}));
Stream<Either<T, R>> rebuild = Stream.of();
for(Either<T,R> e : next){
Stream<? extends Either<T, R>> r = e.fold(s -> {
newValue[0] = true;

return fn.apply(s);
},
p -> {
newValue[0] = false;
return Stream.of(e);
});
rebuild = Stream.concat(rebuild,r);
}
next = rebuild;
if(!newValue[0])
break;

Expand All @@ -92,13 +97,21 @@ public static <T,R> Stream<R> tailRecXor(T initial, Function<? super T, ? exten
boolean newValue[] = {true};
for(;;){

next = next.flatMap(e -> e.visit(s -> {
newValue[0]=true;
return fn.apply(s); },
p -> {
newValue[0]=false;
return Stream.of(e);
}));
Stream<Xor<T, R>> rebuild = Stream.of();
for(Xor<T,R> e : next){
Stream<? extends Xor<T, R>> r = e.visit(s -> {
newValue[0] = true;

return fn.apply(s);
},
p -> {
newValue[0] = false;
return Stream.of(e);
});
rebuild = Stream.concat(rebuild,r);
}

next = rebuild;
if(!newValue[0])
break;

Expand All @@ -114,18 +127,20 @@ public static <T,R> R foldRightUnsafe(Stream<T> stream,R identity, BiFunction<?
return fn.apply(stream.head(),foldRight(stream.tail(),identity,fn));
}

public static <T,R> R foldRight(Stream<T> stream,R identity, BiFunction<? super T, ? super R, ? extends R> fn){
return foldRightRec(stream,Eval.now(identity),(a,b)-> b.map(b2->fn.apply(a,b2))).get();
}
private static <T,R> Eval<R> foldRightRec(Stream<T> stream,Eval<R> identity, BiFunction<? super T, ? super Eval<R>, ? extends Eval<R>> fn){
public static <T,R> R foldRight(Stream<T> stream,R identity, BiFunction<? super T, ? super R, ? extends R> p){
class Step{
public Trampoline<R> loop(Stream<T> s, Function<? super R, ? extends Trampoline<R>> fn){
if (s.isEmpty())
return fn.apply(identity);
return Trampoline.more(()->loop(s.tail(), rem -> Trampoline.more(() -> fn.apply(p.apply(s.head(), rem)))));

if(stream.isEmpty())
return identity;
else
return identity.flatMap(i-> fn.apply(stream.head(), foldRightRec(stream.tail(), identity, fn)));
}
}
return new Step().loop(stream,i->Trampoline.done(i)).result();
}



public static <T> AnyMSeq<stream,T> anyM(Stream<T> option) {
return AnyM.ofSeq(option, stream.INSTANCE);
}
Expand Down
39 changes: 39 additions & 0 deletions cyclops-vavr/src/test/java/cyclops/MonadRecTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cyclops;

import com.aol.cyclops.vavr.hkt.ListKind;
import com.aol.cyclops2.hkt.Higher;
import cyclops.async.Future;

import cyclops.companion.vavr.Lists;
import cyclops.companion.vavr.Streams;
import cyclops.control.Xor;
import cyclops.monads.VavrWitness;
import cyclops.monads.VavrWitness.list;
import cyclops.monads.VavrWitness.stream;
import cyclops.stream.ReactiveSeq;
import cyclops.typeclasses.monad.MonadRec;
import io.vavr.collection.List;
import io.vavr.collection.Stream;
import org.junit.Test;

import java.util.Optional;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;


public class MonadRecTest {

@Test
public void listTest(){
MonadRec<list> mr = Lists.Instances.monadRec();
List<Integer> l = Lists.tailRecXor(0, i -> i < 100_000 ? List.of(Xor.secondary(i + 1)) : List.of(Xor.primary(i + 1)));
assertThat(l,equalTo(List.of(100_001)));
}
@Test
public void streamTest(){
MonadRec<stream> mr = Streams.Instances.monadRec();
Stream<Integer> l = Streams.tailRecXor(0, i -> i < 100_000 ? Stream.of(Xor.secondary(i + 1)) : Stream.of(Xor.primary(i + 1)));
assertThat(l,equalTo(Stream.of(100_001)));
}
}
24 changes: 24 additions & 0 deletions cyclops-vavr/src/test/java/cyclops/StreamsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cyclops;

import cyclops.companion.vavr.Streams;
import io.vavr.collection.Stream;
import org.junit.Test;


import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

/**
* Created by johnmcclean on 08/08/2017.
*/
public class StreamsTest {

@Test
public void simpleFoldRight(){
assertThat(Streams.foldRight(Stream.of(1,2,3),0,(a,b)->a+b),equalTo(6));
}
@Test
public void stackBuster(){
assertThat(Streams.foldRight(Stream.range(0,100_000),0,(a,b)->a+b),equalTo(704982704));
}
}

0 comments on commit 50b2bfa

Please sign in to comment.