Skip to content

Commit

Permalink
refactor: use min_by_key in decision making helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mpizenberg authored and Eh2406 committed Nov 15, 2020
1 parent caf5059 commit bb86428
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,22 +278,17 @@ where
I: Iterator<Item = V>,
F: Fn(&P) -> I,
{
let mut package: Option<(T, _)> = None;
let mut min_key = usize::MAX;
for (p, term) in potential_packages {
let key = list_available_versions(p.borrow())
.filter(|v| term.borrow().contains(v.borrow()))
.count();

if key < min_key {
min_key = key;
package = Some((p, term));
}
}
let (package, term) = package.expect("potential_packages gave us an empty iterator");
let ver =
list_available_versions(package.borrow()).find(|v| term.borrow().contains(v.borrow()));
(package, ver)
let count_valid = |(p, range): &(T, U)| {
list_available_versions(p.borrow())
.filter(|v| range.borrow().contains(v.borrow()))
.count()
};
let (pkg, range) = potential_packages
.min_by_key(count_valid)
.expect("potential_packages gave us an empty iterator");
let version =
list_available_versions(pkg.borrow()).find(|v| range.borrow().contains(v.borrow()));
(pkg, version)
}

/// A basic implementation of [DependencyProvider].
Expand Down

0 comments on commit bb86428

Please sign in to comment.