diff --git a/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Streams.java b/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Streams.java index fc1fd484..31ba69bc 100644 --- a/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Streams.java +++ b/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Streams.java @@ -5,10 +5,8 @@ import cyclops.companion.CompletableFutures.CompletableFutureKind; import cyclops.companion.Optionals; import cyclops.companion.Optionals.OptionalKind; -import cyclops.control.Eval; -import cyclops.control.Maybe; +import cyclops.control.*; import cyclops.control.Reader; -import cyclops.control.Xor; import cyclops.conversion.functionaljava.FromJDK; import cyclops.conversion.functionaljava.FromJooqLambda; import cyclops.monads.*; @@ -103,15 +101,16 @@ public static Stream tailRecXor(T initial, Function R foldRight(Stream stream,R identity, BiFunction fn){ - return foldRightRec(stream,Eval.now(identity),(a,b)-> b.map(b2->fn.apply(a,b2))).get(); - } - private static Eval foldRightRec(Stream stream,Eval identity, BiFunction, ? extends Eval> fn){ + public static R foldRight(Stream stream,R identity, BiFunction p){ + class Step{ + public Trampoline loop(Stream s, Function> fn){ + if (s.isEmpty()) + return fn.apply(identity); + return Trampoline.more(()->loop(s.tail()._1(), 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()._1(), identity, fn))); + } + } + return new Step().loop(stream,i->Trampoline.done(i)).result(); } /** * Perform a For Comprehension over a Stream, accepting 3 generating functions.