Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dropLast(n) #44

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### 0.6.0
+ TBD
+ Implement `dropLast(n)`

### 0.5.0
+ Implement `reverse()`
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ implementation("com.ginsberg:gatherers4j:0.6.0")
| `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` |
| `dropLast(n)` | Keep all but the last `n` elements of the stream |
| `exactSize(n)` | Ensure the stream is exactly `n` elements long, or throw an `IllegalStateException` |
| `filterWithIndex(predicate)` | Filter the stream with the given `predicate`, which takes an `element` and its `index` |
| `interleave(stream)` | Creates a stream of alternating objects from the input stream and the argument stream |
Expand Down Expand Up @@ -139,6 +140,16 @@ Stream
// [Person("Todd", "Ginsberg"), Person("Emma", "Ginsberg")]
```

#### Keep all but the last `n` elements

```java
Stream.of("A", "B", "C", "D", "E")
.gather(Gatherers4j.dropLast(2))
.toList();

// ["A", "B", "C"]
```

#### Ensure the stream is exactly `n` elements long

```java
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/ginsberg/gatherers4j/DropLastGatherer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2024 Todd Ginsberg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.ginsberg.gatherers4j;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Gatherer;

public class DropLastGatherer<INPUT> implements Gatherer<INPUT, DropLastGatherer.State<INPUT>, INPUT> {

private final int count;

DropLastGatherer(final int count) {
if (count <= 0) {
throw new IllegalArgumentException("DropLast count must be positive");
}
this.count = count;
}

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

@Override
public Integrator<State<INPUT>, INPUT, INPUT> integrator() {
return (state, element, downstream) -> {
state.elements.add(element);
return !downstream.isRejecting();
};
}

@Override
public BiConsumer<State<INPUT>, Downstream<? super INPUT>> finisher() {
return (inputState, downstream) -> {
for (int i = 0; i < inputState.elements.size() - count && !downstream.isRejecting(); i++) {
downstream.push(inputState.elements.get(i));
}
};
}

public static class State<INPUT> {
final List<INPUT> elements = new ArrayList<>();
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/ginsberg/gatherers4j/Gatherers4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ public static <INPUT> ThrottlingGatherer<INPUT> debounce(final int amount, final
return new DistinctGatherer<>(function);
}

/**
* Keep all elements except the last <code>count</code> elements of the stream.
*
* @param count A positive number of elements to drop from the end of the stream
*/
public static <INPUT> DropLastGatherer<INPUT> dropLast(final int count) {
return new DropLastGatherer<>(count);
}

/**
* Ensure the input stream is exactly <code>size</code> elements long, and emit all elements
* if so. If not, throw an <code>IllegalStateException</code>.
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/com/ginsberg/gatherers4j/DropLastGathererTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2024 Todd Ginsberg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.ginsberg.gatherers4j;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class DropLastGathererTest {

@Test
void countMustBePositive() {
assertThatThrownBy(() -> Stream.of("A")
.gather(Gatherers4j.dropLast(0)).toList())
.isInstanceOf(IllegalArgumentException.class);
}

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

// Act
final List<String> output = input.gather(Gatherers4j.dropLast(2)).toList();

// Assert
assertThat(output).containsExactly("A");
}

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

// Act
final List<String> output = input.gather(Gatherers4j.dropLast(4)).toList();

// Assert
assertThat(output).isEmpty();
}

}
Loading