@Test public void testFold() { Cofree<Option_, Integer> cofree = Cofree.unfold(OptionInstances.functor(), 0, a -> (a > 100) ? Option.<Integer>none() : Option.some(a + 1)); assertEquals(5151, cofree.<Integer>fold(EvalInstances.applicative(), OptionInstances.traverse(), (a, fb) -> Eval.later(() -> fb.fix(toOption()).fold(() -> a, x -> x + a))).value()); }
public <B> Eval<B> fold(Applicative<Eval_> applicative, Traverse<F> traverse, Function2<A, Kind<F, B>, Eval<B>> mapper) { Eval<Kind<F, B>> eval = traverse.traverse(applicative, tailForced(), c -> c.fold(applicative, traverse, mapper)) .fix(toEval()); return eval.flatMap(fb -> mapper.apply(extract(), fb)); }
Cofree implements CofreeOf<F, A> { public <B> Eval<B> fold(Applicative<Eval_> applicative, Traverse<F> traverse, Function2<A, Kind<F, B>, Eval<B>> mapper) { Eval<Kind<F, B>> eval = traverse.traverse(applicative, tailForced(), c -> c.fold(applicative, traverse, mapper)) .fix(toEval()); return eval.flatMap(fb -> mapper.apply(extract(), fb)); } }
Cofree implements CofreeOf<F, A> { public <B> Eval<B> fold(Applicative<Eval_> applicative, Traverse<F> traverse, Function2<A, Kind<F, B>, Eval<B>> mapper) { Eval<Kind<F, B>> eval = traverse.traverse(applicative, tailForced(), c -> c.fold(applicative, traverse, mapper)) .fix(toEval()); return eval.flatMap(fb -> mapper.apply(extract(), fb)); } private  Cofree(Functor<F> functor, A head, Eval<Kind<F, Cofree<F, A>>> tail); }
Cofree implements CofreeOf<F, A> { public <B> Eval<B> fold(Applicative<Eval_> applicative, Traverse<F> traverse, Function2<A, Kind<F, B>, Eval<B>> mapper) { Eval<Kind<F, B>> eval = traverse.traverse(applicative, tailForced(), c -> c.fold(applicative, traverse, mapper)) .fix(toEval()); return eval.flatMap(fb -> mapper.apply(extract(), fb)); } private  Cofree(Functor<F> functor, A head, Eval<Kind<F, Cofree<F, A>>> tail); A extract(); Kind<F, Cofree<F, A>> tailForced(); Cofree<F, A> runTail(); Cofree<F, A> run(); Cofree<F, B> map(Function1<? super A, ? extends B> mapper); Cofree<F, B> coflatMap(Function1<? super Cofree<F, ? extends A>, ? extends B> mapper); Eval<B> fold(Applicative<Eval_> applicative, Traverse<F> traverse, Function2<A, Kind<F, B>, Eval<B>> mapper); Eval<B> reduce(Applicative<Eval_> applicative, Traverse<F> traverse,
                            Function1<A, B> initial, Operator2<B> combine); Eval<String> reduceToString(Applicative<Eval_> applicative, Traverse<F> traverse, Operator2<String> join); Cofree<F, B> transform(Function1<? super A, ? extends B> headMap, 
      Function1<? super Cofree<F, ? extends A>, ? extends Cofree<F, ? extends B>> tailMap); static Cofree<F, A> unfold(Functor<F> functor, A head, Function1<A, Kind<F, A>> unfold); static Cofree<F, A> of(Functor<F> functor, A head, Eval<Kind<F, Cofree<F, A>>> tail); }
Cofree implements CofreeOf<F, A> { public <B> Eval<B> fold(Applicative<Eval_> applicative, Traverse<F> traverse, Function2<A, Kind<F, B>, Eval<B>> mapper) { Eval<Kind<F, B>> eval = traverse.traverse(applicative, tailForced(), c -> c.fold(applicative, traverse, mapper)) .fix(toEval()); return eval.flatMap(fb -> mapper.apply(extract(), fb)); } private  Cofree(Functor<F> functor, A head, Eval<Kind<F, Cofree<F, A>>> tail); A extract(); Kind<F, Cofree<F, A>> tailForced(); Cofree<F, A> runTail(); Cofree<F, A> run(); Cofree<F, B> map(Function1<? super A, ? extends B> mapper); Cofree<F, B> coflatMap(Function1<? super Cofree<F, ? extends A>, ? extends B> mapper); Eval<B> fold(Applicative<Eval_> applicative, Traverse<F> traverse, Function2<A, Kind<F, B>, Eval<B>> mapper); Eval<B> reduce(Applicative<Eval_> applicative, Traverse<F> traverse,
                            Function1<A, B> initial, Operator2<B> combine); Eval<String> reduceToString(Applicative<Eval_> applicative, Traverse<F> traverse, Operator2<String> join); Cofree<F, B> transform(Function1<? super A, ? extends B> headMap, 
      Function1<? super Cofree<F, ? extends A>, ? extends Cofree<F, ? extends B>> tailMap); static Cofree<F, A> unfold(Functor<F> functor, A head, Function1<A, Kind<F, A>> unfold); static Cofree<F, A> of(Functor<F> functor, A head, Eval<Kind<F, Cofree<F, A>>> tail);  }