Skip to content

Feature request: equivalent of Guava's FluentIterable filter(Class<T> type) method #3224

@s7oev

Description

@s7oev

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions