@Test public void unfold(){ Cofree<identity, Integer> unfolding = Cofree.unfold(functor(), i, next->{ i++; return Identity.of(next + 1);}); assertThat(i,equalTo(0)); Higher<identity, Cofree<identity, Integer>> next = unfolding.tailForced(); assertThat(i,equalTo(1)); Identity<Cofree<identity, Integer>> cof = Identity.narrowK(next); cof.map(c->c.tailForced()); assertThat(i,equalTo(2)); }
public static <W,T> Cofree<W,T> unfold(Functor<W> functor,T b, Function<? super T, ? extends Higher<W, T>> fn) { return of(functor, b, Eval.later(() -> functor.map_(fn.apply(b), t -> unfold(functor, t, fn)))); }
Cofree implements Supplier<T>, Higher2<cofree,W,T> { public static <W,T> Cofree<W,T> unfold(Functor<W> functor,T b, Function<? super T, ? extends Higher<W, T>> fn) { return of(functor, b, Eval.later(() -> functor.map_(fn.apply(b), t -> unfold(functor, t, fn)))); } }
Cofree implements Supplier<T>, Higher2<cofree,W,T> { public static <W,T> Cofree<W,T> unfold(Functor<W> functor,T b, Function<? super T, ? extends Higher<W, T>> fn) { return of(functor, b, Eval.later(() -> functor.map_(fn.apply(b), t -> unfold(functor, t, fn)))); }  }
Cofree implements Supplier<T>, Higher2<cofree,W,T> { public static <W,T> Cofree<W,T> unfold(Functor<W> functor,T b, Function<? super T, ? extends Higher<W, T>> fn) { return of(functor, b, Eval.later(() -> functor.map_(fn.apply(b), t -> unfold(functor, t, fn)))); }  static Cofree<W,T> of(Functor<W> functor, T head, Eval<Higher<W, Cofree<W, T>>> tail); Higher<W, Cofree<W, T>> tailForced(); Cofree<W,R> map(Function<? super T,? extends R> f); Cofree<W, R> coflatMap(Function<? super Cofree<W, T>,? extends  R> f); Cofree<W, Cofree<W, T>> nest(); Cofree<W, R> transform(Function<? super T,? extends R> f, Function<Cofree<W, T>,Cofree<W, R>> g); Cofree<W, T> mapBranchingRoot(NaturalTransformation<W, W> nat); Cofree<R, T> mapBranchingS(Functor<R> functor,NaturalTransformation<W, R> nat); Cofree<R, T> mapBranchingT(Functor<R> functor,NaturalTransformation<W, R> nat); Cofree<W, T> forceTail(); Cofree<W, T> forceAll(); T extract(); T get(); Eval<R> fold(Traverse<W> traverse, BiFunction<T, Higher<W, R>,Eval<R>> fn); Higher<M,R> visitM(Traverse<W> traverse, Monad<M> monad,BiFunction<? super T,? super Higher<W, R>,Higher<M, R>> fn,
                                      NaturalTransformation<eval, M> inclusion); static Cofree<W,T> unfold(Functor<W> functor,T b, Function<? super T, ? extends Higher<W, T>> fn); static Cofree<W,T> narrowK2(final Higher2<cofree, W,T> cof); static Cofree<W,T> narrowK(final Higher<Higher<cofree, W>,T> cof); }
Cofree implements Supplier<T>, Higher2<cofree,W,T> { public static <W,T> Cofree<W,T> unfold(Functor<W> functor,T b, Function<? super T, ? extends Higher<W, T>> fn) { return of(functor, b, Eval.later(() -> functor.map_(fn.apply(b), t -> unfold(functor, t, fn)))); }  static Cofree<W,T> of(Functor<W> functor, T head, Eval<Higher<W, Cofree<W, T>>> tail); Higher<W, Cofree<W, T>> tailForced(); Cofree<W,R> map(Function<? super T,? extends R> f); Cofree<W, R> coflatMap(Function<? super Cofree<W, T>,? extends  R> f); Cofree<W, Cofree<W, T>> nest(); Cofree<W, R> transform(Function<? super T,? extends R> f, Function<Cofree<W, T>,Cofree<W, R>> g); Cofree<W, T> mapBranchingRoot(NaturalTransformation<W, W> nat); Cofree<R, T> mapBranchingS(Functor<R> functor,NaturalTransformation<W, R> nat); Cofree<R, T> mapBranchingT(Functor<R> functor,NaturalTransformation<W, R> nat); Cofree<W, T> forceTail(); Cofree<W, T> forceAll(); T extract(); T get(); Eval<R> fold(Traverse<W> traverse, BiFunction<T, Higher<W, R>,Eval<R>> fn); Higher<M,R> visitM(Traverse<W> traverse, Monad<M> monad,BiFunction<? super T,? super Higher<W, R>,Higher<M, R>> fn,
                                      NaturalTransformation<eval, M> inclusion); static Cofree<W,T> unfold(Functor<W> functor,T b, Function<? super T, ? extends Higher<W, T>> fn); static Cofree<W,T> narrowK2(final Higher2<cofree, W,T> cof); static Cofree<W,T> narrowK(final Higher<Higher<cofree, W>,T> cof);  }