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

com.jnape.palatable.lambda.functions.builtin.fn2.Take 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.iterators.TakingIterator;

/**
 * Lazily limit the Iterable to n elements by returning an Iterable that stops
 * iteration after the nth element, or the last element of the Iterable, whichever comes
 * first.
 *
 * @param  The Iterable element type
 * @see TakeWhile
 * @see Drop
 */
public final class Take implements Fn2, Iterable> {

    private Take() {
    }

    @Override
    public Iterable apply(Integer n, Iterable as) {
        return () -> new TakingIterator<>(n, as.iterator());
    }

    public static  Take take() {
        return new Take<>();
    }

    public static  Fn1, Iterable> take(int n) {
        return Take.take().apply(n);
    }

    public static  Iterable take(int n, Iterable as) {
        return Take.take(n).apply(as);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy