com.jnape.palatable.lambda.functions.builtin.fn2.Snoc Maven / Gradle / Ivy
Show all versions of lambda Show documentation
package com.jnape.palatable.lambda.functions.builtin.fn2;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.Fn2;
import com.jnape.palatable.lambda.iterators.SnocIterator;
/**
* Opposite of {@link Cons}: lazily append an element to the end of the given {@link Iterable}.
*
* Note that obtaining both laziness and stack-safety is particularly tricky here, and requires an initial eager
* deforestation of O(k)
traversals where k
is the number of contiguously nested
* {@link Snoc}s.
*
* @param the Iterable element type
*/
public final class Snoc implements Fn2, Iterable> {
private static final Snoc INSTANCE = new Snoc();
private Snoc() {
}
@Override
public Iterable apply(A a, Iterable as) {
return () -> new SnocIterator<>(a, as);
}
@SuppressWarnings("unchecked")
public static Snoc snoc() {
return INSTANCE;
}
public static Fn1, Iterable> snoc(A a) {
return Snoc.snoc().apply(a);
}
public static Iterable snoc(A a, Iterable as) {
return snoc(a).apply(as);
}
}