Created
July 12, 2022 23:27
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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