All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jnape.palatable.lambda.functions.builtin.fn2.InGroupsOf Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
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 Iterables 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