Skip to content

Commit

Permalink
Expand readme (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored Oct 15, 2024
1 parent bdb17a7 commit c25805b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ Java's Stream API is a powerful tool for processing collections of data. However
Whenever possible, the library follows Project Reactor's naming conventions.

Provided `Gatherers`:
- `MoreGatherers.last(int)`
- `MoreGatherers.sampling(int)`
- `MoreGatherers.zip(Iterator<T2>)`
- `MoreGatherers.zip(Iterator<T2>, BiFunction<T1,T2>)`
- `MoreGatherers.zip(Stream<T2>)`
- `MoreGatherers.zip(Stream<T2>, BiFunction<T1,T2>)`
- `MoreGatherers.zipWithIterable(Iterable<T2>)`
- `MoreGatherers.zipWithIterable(Iterable<T2>, BiFunction<T1,T2>)`
- `MoreGatherers.zipWithIndex()`
- `MoreGatherers.zipWithIndex(BiFunction<Long,T>)`
- `MoreGatherers.distinctBy(Function<T, R>)`
- `MoreGatherers.distinctByKeepLast(Function<T, R>)`
- `MoreGatherers.distinctUntilChanged()`
- `MoreGatherers.distinctUntilChanged(Function<T, R>)`
- `MoreGatherers.last(int)`: takes last `n` elements from the stream
- `MoreGatherers.sampling(int)`: takes every `n`-th element from the stream
- `MoreGatherers.zip(Iterator<T2>)`: zips `Stream` elements with elements from the provided `Iterator`
- `MoreGatherers.zip(Iterator<T2>, BiFunction<T1,T2>)`: zips `Stream` elements with elements from the provided `Iterator` using a custom zipper function
- `MoreGatherers.zip(Stream<T2>)`: zips `Stream` elements with elements from the provided `Stream`
- `MoreGatherers.zip(Stream<T2>, BiFunction<T1,T2>)`: zips `Stream` elements with elements from the provided `Stream` using a custom zipper function
- `MoreGatherers.zipWithIterable(Iterable<T2>)`: zips `Stream` elements with elements from the provided `Iterable`
- `MoreGatherers.zipWithIterable(Iterable<T2>, BiFunction<T1,T2>)`: zips elements with elements from the provided `Iterable` using a custom zipper function
- `MoreGatherers.zipWithIndex()`: zips `Stream` elements with their index
- `MoreGatherers.zipWithIndex(BiFunction<Long,T>)`: zips `Stream` elements with their index using a custom zipper function
- `MoreGatherers.distinctBy(Function<T, R>)`: takes distinct elements based on a key extractor function
- `MoreGatherers.distinctByKeepLast(Function<T, R>)`: takes distinct elements based on a key extractor function, keeping the last occurrence
- `MoreGatherers.distinctUntilChanged()`: takes elements until a change is detected
- `MoreGatherers.distinctUntilChanged(Function<T, R>)`: takes elements until a change is detected based on a key extractor function

### Philosophy

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/pivovarit/gatherers/MoreGatherers.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private MoreGatherers() {
*
* @return a {@link Gatherer} that pairs elements with their corresponding index
*/
public static <T> Gatherer<T, ?, Map.Entry<Long, T>> zipWithIndex() {
public static <T> Gatherer<T, ?, Map.Entry<T, Long>> zipWithIndex() {
return new ZipWithIndexGatherer<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
import java.util.function.Supplier;
import java.util.stream.Gatherer;

record ZipWithIndexGatherer<T>() implements Gatherer<T, AtomicLong, Map.Entry<Long, T>> {
record ZipWithIndexGatherer<T>() implements Gatherer<T, AtomicLong, Map.Entry<T, Long>> {

@Override
public Supplier<AtomicLong> initializer() {
return AtomicLong::new;
}

@Override
public Integrator<AtomicLong, T, Map.Entry<Long, T>> integrator() {
public Integrator<AtomicLong, T, Map.Entry<T, Long>> integrator() {
return Integrator.ofGreedy((state, element, downstream) -> {
downstream.push(Map.entry(state.getAndIncrement(), element));
downstream.push(Map.entry(element, state.getAndIncrement()));
return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ void shouldZipWithIndex() {
assertThat(Stream.of("a", "b", "c")
.gather(zipWithIndex()))
.containsExactly(
entry(0L, "a"),
entry(1L, "b"),
entry(2L, "c")
entry("a", 0L),
entry("b", 1L),
entry("c", 2L)
);
}
}

0 comments on commit c25805b

Please sign in to comment.