Skip to content

Instantly share code, notes, and snippets.

@lukaseder
Last active September 8, 2020 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaseder/d713cce142c5475c00dba959f1bc58e0 to your computer and use it in GitHub Desktop.
Save lukaseder/d713cce142c5475c00dba959f1bc58e0 to your computer and use it in GitHub Desktop.
SimplifyThis.java
List<MappedTable> mappedTables1() {
if (mappedTables == null) {
mappedTables = T_BOOK
.getSchema()
.getTables()
.stream()
.map(t -> {
Table<?> l = lookupTable(t.getName());
return l == null ? null : new MappedTable().withInput(t.getName()).withOutput(l.getName());
})
.filter(Predicate.not(Objects::isNull))
.collect(Collectors.toList());
}
return mappedTables;
}
List<MappedTable> mappedTables2() {
if (mappedTables == null) {
mappedTables = T_BOOK
.getSchema()
.getTables()
.stream()
.flatMap(t ->
Optional.ofNullable(lookupTable(t.getName()))
.map(l -> new MappedTable().withInput(t.getName()).withOutput(l.getName()))
.stream()
)
.filter(Predicate.not(Objects::isNull))
.collect(Collectors.toList());
}
return mappedTables;
}
@Mordavolt
Copy link

    //Only improvement is .map(Something::getName)
    List<MappedTable> mappedTables2() {
        if (mappedTables == null) {
            mappedTables = T_BOOK
                    .getSchema()
                    .getTables()
                    .stream()
                    .map(Something::getName)
                    .flatMap(name ->
                            Optional.ofNullable(lookupTable(name))
                                    .map(l -> new MappedTable().withInput(name).withOutput(l.getName()))
                                    .stream()
                    )
                    .filter(Predicate.not(Objects::isNull))
                    .collect(Collectors.toList());
        }

        return mappedTables;
    }

    //If only we had tuples :( 
    // This one is cleaner, but recalculates lookupTable(name), so a no go.
    List<MappedTable> mappedTables2() {
        if (mappedTables == null) {
            mappedTables = T_BOOK
                    .getSchema()
                    .getTables()
                    .stream()
                    .map(Something::getName)
                    .filter(name -> lookupTable(name) != null)
                    .map(name -> new MappedTable()
                            .withInput(name)
                            .withOutput(lookupTable(name))
                            .getName()))
                    .collect(Collectors.toList());
        }

        return mappedTables;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment