com.jnape.palatable.lambda.functions.builtin.fn2.InGroupsOf Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lambda Show documentation
Show all versions of lambda Show documentation
Functional patterns for Java
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.internal.iteration.GroupingIterator;
/**
* Lazily group the Iterable
by returning an Iterable
of smaller Iterable
s of
* size k
. Note that groups are not padded; that is, if k >= n
, where
* n
is the number of remaining elements, the final Iterable
will have only n
* elements.
*
* @param The Iterable element type
*/
public final class InGroupsOf implements Fn2, Iterable>> {
private static final InGroupsOf> INSTANCE = new InGroupsOf<>();
private InGroupsOf() {
}
@Override
public Iterable> checkedApply(Integer k, Iterable as) {
return () -> new GroupingIterator<>(k, as.iterator());
}
@SuppressWarnings("unchecked")
public static InGroupsOf inGroupsOf() {
return (InGroupsOf) INSTANCE;
}
public static Fn1, Iterable>> inGroupsOf(Integer k) {
return InGroupsOf.inGroupsOf().apply(k);
}
public static Iterable> inGroupsOf(Integer k, Iterable as) {
return InGroupsOf.inGroupsOf(k).apply(as);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy