-
-
Notifications
You must be signed in to change notification settings - Fork 659
Open
Description
I have a lot of use-cases in my codebase with the following pattern:
myVavrSeq
.filter(SomeClass.class::isInstance)
.map(SomeClass.class::cast)
.map(objFromSomeClass -> /* ... */);Obviously the above is perfectly fine, but I was thinking filter based on + map to a class is a really common pattern, and after some researching, I saw that the Guava library does offer it as a single method (docs).
Here is an example:
@Test
void example() {
interface MyInterface {}
class MyClassA implements MyInterface {}
class MyClassB implements MyInterface {}
MyClassA myObjA1 = new MyClassA(), myObjA2 = new MyClassA();
MyClassB myObjB1 = new MyClassB(), myObjB2 = new MyClassB();
java.util.List<MyInterface> myList = java.util.List.of(myObjA1, myObjB1, myObjA2, myObjB2);
FluentIterable<MyClassA> myGuavaFilteredIterable = FluentIterable.from(myList).filter(MyClassA.class);
io.vavr.collection.List<MyClassA> myVavrFilteredList = io.vavr.collection.List.ofAll(myList)
.filter(MyClassA.class::isInstance)
.map(MyClassA.class::cast);
assertThat(myGuavaFilteredIterable)
.containsExactlyElementsOf(myVavrFilteredList)
.containsExactly(myObjA1, myObjA2);
}If I am not mistaken, there is no equivalent in vavr. So this issue is a feature request for this. It would simplify the code from the beginning down to:
myVavrSeq
.filter(SomeClass.class)
.map(objFromSomeClass -> /* ... */);Obviously, the vavr community can share their opinion on it, if you agree and think it makes sense, or disagree, thinking it's not really useful, do comment please.
If it is accepted as a new feature, I'd also be interested in contributing it.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels