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

style: promote idiomatic functional Rust #372

Closed
Closed
Changes from 1 commit
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
17 changes: 9 additions & 8 deletions src/menu.rs
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method Avg t @ 10000 elements
original for 6.4166 µs
filter()/count() (supposed O(n²) 4.5204 µs
filter()/fold() (last one) 3.6868 µs

Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ pub fn context_menu<'a>(
}
}

let selected = tab.items_opt().into_iter().fold(SelectionCounter::new(),
|mut selections, items| {
let selected_iter = items.into_iter().filter(|i| i.selected);
selections.total_count += selected_iter.clone().count();
selections.dirs_count += selected_iter.filter(|i| i.metadata.is_dir()).count();
selections
}
);
let selected = tab.items_opt().map_or(SelectionCounter::new(), |items| {
items.into_iter()
.filter(|i| i.selected)
.fold(SelectionCounter::new(), |mut counter, selection| {
counter.total_count += 1;
selection.metadata.is_dir().then(|| counter.dirs_count += 1);
counter
})
});

let mut children: Vec<Element<_>> = Vec::with_capacity(16);
match tab.location {
Expand Down