Skip to content

Commit

Permalink
Add starting point to indexed
Browse files Browse the repository at this point in the history
  • Loading branch information
tginsberg committed Aug 15, 2024
1 parent 129991e commit cd9b61b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 17 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ implementation("com.ginsberg:gatherers4j:0.1.0")
# Gatherers In This Library

### Streams
| Function | Purpose |
|---------------------------|------------------------------------------------------------------------------------------------------------------------------------|
| Function | Purpose |
|---------------------------|-----------------------------------------------------------------------------------------------------------------------|
| `concat(stream)` | Creates a stream which is the concatenation of the source stream and the given stream, which must be of the same type |
| `dedupeConsecutive()` | Remove consecutive duplicates from a stream |
| `dedupeConsecutiveBy(fn)` | Remove consecutive duplicates from a stream as returned by `fn` |
| `distinctBy(fn)` | Emit only distinct elements from the stream, as measured by `fn` |
| `interleave(stream)` | Creates a stream of alternating objects from the input stream and the argument stream |
| `last(n)` | Constrain the stream to the last `n` values |
| `withIndex()` | Maps all elements of the stream as-is, along with their 0-based index. |
| `zipWith(stream)` | Creates a stream of `Pair` objects whose values come from the input stream and argument stream |
| `zipWithNext()` | Creates a stream of `List` objects via a sliding window of width 2 and stepping 1 | |
| `dedupeConsecutive()` | Remove consecutive duplicates from a stream |
| `dedupeConsecutiveBy(fn)` | Remove consecutive duplicates from a stream as returned by `fn` |
| `distinctBy(fn)` | Emit only distinct elements from the stream, as measured by `fn` |
| `interleave(stream)` | Creates a stream of alternating objects from the input stream and the argument stream |
| `last(n)` | Constrain the stream to the last `n` values |
| `withIndex()` | Maps all elements of the stream as-is along with their 0-based index |
| `withIndexStartingAt(n)` | Maps all elements of the stream as-is along with an index starting at the number specified |
| `zipWith(stream)` | Creates a stream of `Pair` objects whose values come from the input stream and argument stream |
| `zipWithNext()` | Creates a stream of `List` objects via a sliding window of width 2 and stepping 1 |

### Mathematics/Statistics
| Function | Purpose |
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/ginsberg/gatherers4j/Gatherers4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,21 @@ public static <INPUT> BigDecimalSimpleMovingAverageGatherer<INPUT> simpleMovingA
}

/**
* Maps all elements of the stream as-is, along with their 0-based index.
* Maps all elements of the stream as-is along with their 0-based index.
*/
public static <INPUT> Gatherer<INPUT, ?, IndexedValue<INPUT>> withIndex() {
public static <INPUT> IndexingGatherer<INPUT> withIndex() {
return new IndexingGatherer<>();
}

/**
* Maps all elements of the stream as-is along with and index, starting at the specified number.
*
* @param start The starting index to use
*/
public static <INPUT> IndexingGatherer<INPUT> withIndexStartingAt(final long start) {
return new IndexingGatherer<INPUT>().startingAt(start);
}

/**
* Creates a stream of `Pair` objects whose values come from the input stream and argument stream
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ginsberg/gatherers4j/IndexedValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@

package com.ginsberg.gatherers4j;

public record IndexedValue<TYPE>(int index, TYPE value) {
public record IndexedValue<TYPE>(long index, TYPE value) {
}
18 changes: 14 additions & 4 deletions src/main/java/com/ginsberg/gatherers4j/IndexingGatherer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,31 @@
public class IndexingGatherer<INPUT>
implements Gatherer<INPUT, IndexingGatherer.State, IndexedValue<INPUT>> {

private long start = 0;

IndexingGatherer() {
}

public IndexingGatherer<INPUT> startingAt(long start) {
this.start = start;
return this;
}

@Override
public Supplier<IndexingGatherer.State> initializer() {
return State::new;
return () -> new State(start);
}

@Override
public Integrator<IndexingGatherer.State, INPUT, IndexedValue<INPUT>> integrator() {
return (state, element, downstream) -> downstream
.push(new IndexedValue<>(state.index++, element));
return (state, element, downstream) -> downstream.push(new IndexedValue<>(state.index++, element));
}

public static class State {
int index;
long index;

public State(long start) {
this.index = start;
}
}
}
38 changes: 38 additions & 0 deletions src/test/java/com/ginsberg/gatherers4j/IndexingGathererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,42 @@ void integerWithIndex() {
);
}

@Test
void startingAtNonZero() {
// Arrange
final Stream<String> input = Stream.of("A", "B", "C");

// Act
final List<IndexedValue<String>> output = input
.gather(Gatherers4j.withIndexStartingAt(100))
.toList();

// Assert
assertThat(output)
.containsExactly(
new IndexedValue<>(100, "A"),
new IndexedValue<>(101, "B"),
new IndexedValue<>(102, "C")
);
}

@Test
void startingAtNonZeroTypeWitnessed() {
// Arrange
final Stream<String> input = Stream.of("A", "B", "C");

// Act
final List<IndexedValue<String>> output = input
.gather(Gatherers4j.<String>withIndex().startingAt(100))
.toList();

// Assert
assertThat(output)
.containsExactly(
new IndexedValue<>(100, "A"),
new IndexedValue<>(101, "B"),
new IndexedValue<>(102, "C")
);
}

}

0 comments on commit cd9b61b

Please sign in to comment.