Skip to content

changing solutions/iterators/iterators3.rs update issue#2212 #2246

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
224 changes: 223 additions & 1 deletion dev-Cargo.toml
38 changes: 29 additions & 9 deletions solutions/18_iterators/iterators3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,33 @@ fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
Ok(a / b)
}

fn result_with_list() -> Result<Vec<i64>, DivisionError> {
fn result_with_list(numbers: Vec<i64>, b: i64) -> Result<Vec<i64>, DivisionError> {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
let numbers = [27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
let division_results = numbers.into_iter().map(|n| divide(n, b));
// Collects to the expected return type. Returns the first error in the
// division results (if one exists).
division_results.collect()
match division_results.find(|n| match n {
Ok(num) => return false,
Err(e) => return true,
}) {
Some(r) => return Err(r.unwrap_err()),
None => {
let ans: Vec<i64> = division_results
.map(|n| {
return match n {
Ok(num) => return num,
Err(e) => return -1,
};
})
.collect();
return Ok(ans);
}
}
}

fn list_of_results() -> Vec<Result<i64, DivisionError>> {
fn list_of_results(numbers: Vec<i64>, b: i64) -> Vec<Result<i64, DivisionError>> {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
let numbers = [27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
let division_results = numbers.into_iter().map(|n| divide(n, b));
// Collects to the expected return type.
division_results.collect()
}
Expand Down Expand Up @@ -76,11 +90,17 @@ mod tests {

#[test]
fn test_result_with_list() {
assert_eq!(result_with_list().unwrap(), [1, 11, 1426, 3]);
assert_eq!(
result_with_list(vec![27, 297, 38502, 81], 27).unwrap(),
[1, 11, 1426, 3]
);
}

#[test]
fn test_list_of_results() {
assert_eq!(list_of_results(), [Ok(1), Ok(11), Ok(1426), Ok(3)]);
assert_eq!(
list_of_results(vec![27, 297, 38502, 81], 27),
[Ok(1), Ok(11), Ok(1426), Ok(3)]
);
}
}
Loading