all_errors
This commit is contained in:
parent
029da815ac
commit
3f27099e17
@ -12,6 +12,7 @@
|
|||||||
- [Chapter 2](./chapter_2.md)
|
- [Chapter 2](./chapter_2.md)
|
||||||
- [AnyStr](./exercises/anystr.md)
|
- [AnyStr](./exercises/anystr.md)
|
||||||
- [Mode](./exercises/mode.md)
|
- [Mode](./exercises/mode.md)
|
||||||
|
- [All Errors](./exercises/all_errors.md)
|
||||||
- [Chapter 3](./chapter_3.md)
|
- [Chapter 3](./chapter_3.md)
|
||||||
- [Composition](./exercises/composition.md)
|
- [Composition](./exercises/composition.md)
|
||||||
|
|
||||||
|
36
src/exercises/all_errors.md
Normal file
36
src/exercises/all_errors.md
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
# Report all errors (or only the first error)
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# trait Collects {
|
||||||
|
# type Error;
|
||||||
|
# type Output;
|
||||||
|
# fn push(&self, trace: Vec<i32>, next: i32) -> Result<Vec<i32>, Self::Error>;
|
||||||
|
# fn regularize(&self, result: Result<Result<Vec<i32>, Vec<i32>>, Self::Error>) -> Result<Vec<i32>, Self::Output>;
|
||||||
|
# }
|
||||||
|
# struct First;
|
||||||
|
# struct All;
|
||||||
|
# impl Collects for First {
|
||||||
|
# type Error = i32;
|
||||||
|
# type Output = i32;
|
||||||
|
# fn push(&self, _trace: Vec<i32>, next: i32) -> Result<Vec<i32>, Self::Error> { Err(next) }
|
||||||
|
# fn regularize(&self, result: Result<Result<Vec<i32>, Vec<i32>>, Self::Error>) -> Result<Vec<i32>, Self::Output> { result.map(|r| r.unwrap()) }
|
||||||
|
# }
|
||||||
|
# impl Collects for All {
|
||||||
|
# type Error = std::convert::Infallible;
|
||||||
|
# type Output = Vec<i32>;
|
||||||
|
# fn push(&self, mut trace: Vec<i32>, next: i32) -> Result<Vec<i32>, Self::Error> { trace.push(next); Ok(trace) }
|
||||||
|
# fn regularize(&self, result: Result<Result<Vec<i32>, Vec<i32>>, Self::Error>) -> Result<Vec<i32>, Self::Output> { result.unwrap() }
|
||||||
|
# }
|
||||||
|
# fn first() -> impl Collects<Output = i32> { First }
|
||||||
|
# fn all() -> impl Collects<Output = Vec<i32>> { All }
|
||||||
|
# fn _only_postivie<C: Collects>(numbers: Vec<i32>, c: &C) -> Result<Result<Vec<i32>, Vec<i32>>, C::Error> {
|
||||||
|
# let mut state = Ok(vec![]);
|
||||||
|
# for x in numbers { state = if x > 0 { state.map(|mut vec| {vec.push(x); vec}) } else { Err(c.push(match state { Ok(_) => vec![], Err(vec) => vec }, x)?) } }
|
||||||
|
# Ok(state)
|
||||||
|
# }
|
||||||
|
# fn only_positive<C: Collects>(numbers: Vec<i32>, c: C) -> Result<Vec<i32>, C::Output> { c.regularize(_only_postivie(numbers, &c)) }
|
||||||
|
assert_eq!(only_positive(vec![1, -1, 2, -4], first()), Err(-1));
|
||||||
|
assert_eq!(only_positive(vec![1, 2, 3], first()), Ok(vec![1, 2, 3]));
|
||||||
|
assert_eq!(only_positive(vec![1, -1, 2, -4], all()), Err(vec![-1, -4]));
|
||||||
|
assert_eq!(only_positive(vec![1, 2, 3], all()), Ok(vec![1, 2, 3]));
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user