update all_errors solution
All checks were successful
buildbot/mdbook test Build done.

This commit is contained in:
AF 2024-02-08 17:35:04 +00:00
parent 7d27e3ca5e
commit 850283de10

View File

@ -4,30 +4,39 @@ Make this compile and pass tests:
```rust ```rust
# trait Collects { # trait Collects {
# type Error; # type Fast;
# type Slow;
# type Output; # type Output;
# fn push(&self, trace: Vec<i32>, next: i32) -> Result<Vec<i32>, Self::Error>; # fn create(&self, next: i32) -> Result<Self::Slow, Self::Fast>;
# fn regularize(&self, result: Result<Result<Vec<i32>, Vec<i32>>, Self::Error>) -> Result<Vec<i32>, Self::Output>; # fn update(&self, slow: &mut Self::Slow, next: i32);
# fn regularize(&self, result: Result<Result<Vec<i32>, Self::Slow>, Self::Fast>) -> Result<Vec<i32>, Self::Output>;
# } # }
# struct First; # struct First;
# struct All; # struct All;
# impl Collects for First { # impl Collects for First {
# type Error = i32; # type Fast = i32;
# type Slow = std::convert::Infallible;
# type Output = i32; # type Output = i32;
# fn push(&self, _trace: Vec<i32>, next: i32) -> Result<Vec<i32>, Self::Error> { Err(next) } # fn create(&self, next: i32) -> Result<Self::Slow, Self::Fast> { Err(next) }
# fn regularize(&self, result: Result<Result<Vec<i32>, Vec<i32>>, Self::Error>) -> Result<Vec<i32>, Self::Output> { result.map(|r| r.unwrap()) } # fn update(&self, slow: &mut Self::Slow, _next: i32) { match *slow {} }
# fn regularize(&self, result: Result<Result<Vec<i32>, Self::Slow>, Self::Fast>) -> Result<Vec<i32>, Self::Output> { result.map(|r| r.unwrap_or_else(|slow| match slow {} )) }
# } # }
# impl Collects for All { # impl Collects for All {
# type Error = std::convert::Infallible; # type Fast = std::convert::Infallible;
# type Slow = Vec<i32>;
# type Output = Vec<i32>; # type Output = Vec<i32>;
# fn push(&self, mut trace: Vec<i32>, next: i32) -> Result<Vec<i32>, Self::Error> { trace.push(next); Ok(trace) } # fn create(&self, next: i32) -> Result<Self::Slow, Self::Fast> { let mut slow = vec![]; slow.push(next); Ok(slow) }
# fn regularize(&self, result: Result<Result<Vec<i32>, Vec<i32>>, Self::Error>) -> Result<Vec<i32>, Self::Output> { result.unwrap() } # fn update(&self, slow: &mut Self::Slow, next: i32) { slow.push(next) }
# fn regularize(&self, result: Result<Result<Vec<i32>, Self::Slow>, Self::Fast>) -> Result<Vec<i32>, Self::Output> { result.unwrap_or_else(|fast| match fast {}) }
# } # }
# fn first() -> impl Collects<Output = i32> { First } # fn first() -> impl Collects<Output = i32> { First }
# fn all() -> impl Collects<Output = Vec<i32>> { All } # 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> { # fn _only_postivie<C: Collects>(numbers: Vec<i32>, c: &C) -> Result<Result<Vec<i32>, C::Slow>, C::Fast> {
# let mut state = Ok(vec![]); # 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)?) } } # for x in numbers { state = if x > 0 { state.map(|mut vec| {vec.push(x); vec}) } else { Err(match state {
# Ok(_) => c.create(x)?,
# Err(mut slow) => { c.update(&mut slow, x) ; slow }
# }) } }
# Ok(state) # Ok(state)
# } # }
# fn only_positive<C: Collects>(numbers: Vec<i32>, c: C) -> Result<Vec<i32>, C::Output> { c.regularize(_only_postivie(numbers, &c)) } # fn only_positive<C: Collects>(numbers: Vec<i32>, c: C) -> Result<Vec<i32>, C::Output> { c.regularize(_only_postivie(numbers, &c)) }