我正在尝试删除列表列表中每个列表的最后一项

时间:2018-12-17 18:27:45

标签: list haskell filter

我有一个数据结构,它是一个列表列表。我想删除每个列表的最后一项,最好使用过滤器。我该怎么做?

1 个答案:

答案 0 :(得分:5)

这是一个显示如何执行此操作的示例:

TextField

结果是:

@Override
public void start(Stage primaryStage) throws Exception {
    TableView<Report> table = new TableView<>();

    TableColumn<Report, String> policeCol = new TableColumn<>("Police");
    policeCol.setCellValueFactory(new PropertyValueFactory<>("police"));
    TableColumn<Report, String> medicalCol = new TableColumn<>("Medical");
    medicalCol.setCellValueFactory(new PropertyValueFactory<>("medical"));
    TableColumn<Report, String> fireCol = new TableColumn<>("Fire");
    fireCol.setCellValueFactory(new PropertyValueFactory<>("fire"));
    table.getColumns().addAll(policeCol, medicalCol, fireCol);

    TextField searchInput = new TextField();
    searchInput.setPromptText("Search Response Type");

    // Creation of Search function created here - search via requested service
    FilteredList<Report> filteredReports = new FilteredList<>(getReport());
    searchInput.textProperty().addListener((observavleValue, oldValue, newValue) -> {
        if (newValue == null || newValue.isEmpty()) { // check necessary only once
            filteredReports.setPredicate(null); // equivalent to setting (i->true)
        } else {
            final String lowerCaseFilter = newValue.toLowerCase();

            filteredReports.setPredicate((Predicate<? super Report>) Report -> {
                return Report.getPolice().contains(newValue)
                        || Report.getMedical().toLowerCase().contains(lowerCaseFilter)
                        || Report.getFire().toLowerCase().contains(lowerCaseFilter);
            });
        }
    });
    SortedList<Report> sortedReports = new SortedList<>(filteredReports);
    sortedReports.comparatorProperty().bind(table.comparatorProperty());
    table.setItems(sortedReports);

    Scene scene = new Scene(new VBox(table, searchInput));
    primaryStage.setScene(scene);
    primaryStage.show();
}

public ObservableList<Report> getReport() {
    ObservableList<Report> Reports = FXCollections.observableArrayList();
    Reports.addAll(new Report("ab", "cd", "ef"), new Report("bc", "de", "fg"));
    return Reports;
}

public static class Report {

    private final String fire;
    private final String police;
    private final String medical;

    public Report(String police, String fire, String medical) {
        super();
        this.police = police;
        this.fire = fire;
        this.medical = medical;
    }

    public String getFire() {
        return fire;
    }

    public String getPolice() {
        return police;
    }

    public String getMedical() {
        return medical;
    }

}

请注意,由于map (\xs->if null xs then xs else init xs) [[], [1..3], [4..6]] 无法应用于空列表,因此在删除列表的最后一项之前,需要检查列表中的每个列表是否为空。

相关问题