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