Skip to content

Instantly share code, notes, and snippets.

@ParadoxV5
Created July 12, 2022 23:27
Show Gist options
  • Save ParadoxV5/f0b2e26aa1c41b77916ff9eb999b7ed5 to your computer and use it in GitHub Desktop.
Save ParadoxV5/f0b2e26aa1c41b77916ff9eb999b7ed5 to your computer and use it in GitHub Desktop.
Repeat, but zipping the copies (i.e. repeat at each element of the sequence) instead of concatenating entire repetition copies.
import java.util.*;
import java.util.stream.Stream;
public interface OverlappedRepeat<E> extends Collection<E> {
static <E> Stream<E> overlappedRepeat(Stream<E> stream, int count) {
return stream.flatMap(e -> Stream.generate(()->e).limit(count));
}
static <E> Stream<E> overlappedRepeat(Collection<E> collection, int count) {
return overlappedRepeat(collection.parallelStream(), count);
}
default Stream<E> overlappedRepeat(int count) {
return overlappedRepeat(this, count);
}
static <E> Stream<E> overlappedRepeat(E[] array, int count) {
return overlappedRepeat(Arrays.stream(array), count);
}
static void main(String[] args) {
if(args.length < 2) {
System.err.println("Usage: <n> [items to repeat]");
return;
}
overlappedRepeat(
Arrays.copyOfRange(args, 1, args.length),
Integer.parseUnsignedInt(args[0])
).forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment