/** * Appends the given list to this list. * * @param as The list to append. * @return A new list with the given list appended. */ public NonEmptyList<A> append(final NonEmptyList<A> as) { final List.Buffer<A> b = new List.Buffer<A>(); b.append(tail); b.snoc(as.head); b.append(as.tail); final List<A> bb = b.toList(); return nel(head, bb); }
/** * Binds the given function across each element of this list with a final join. * * @param f The function to apply to each element of this list. * @return A new list after performing the map, then final join. */ public <B> NonEmptyList<B> bind(final F<A, NonEmptyList<B>> f) { final List.Buffer<B> b = new List.Buffer<B>(); final NonEmptyList<B> p = f.f(head); b.snoc(p.head); b.append(p.tail); tail.foreachDoEffect( new Effect1<A>() { public void f(final A a) { final NonEmptyList<B> p = f.f(a); b.snoc(p.head); b.append(p.tail); } }); final List<B> bb = b.toList(); return nel(bb.head(), bb.tail()); }