curry
All checks were successful
buildbot/mdbook test Build done.

This commit is contained in:
AF 2023-12-01 20:01:58 +00:00
parent 0c65a982a1
commit dcd9e832e2
3 changed files with 36 additions and 4 deletions

View File

@ -21,5 +21,6 @@
- [Covariance]()
- [Option, Vec and BoxFuture<'a>]()
- [Err Err](./exercises/err_err.md)
- [Curry](./exercises/curry.md)
[Topics (Spoilers)](./topics.md)

30
src/exercises/curry.md Normal file
View File

@ -0,0 +1,30 @@
# Currying
Implement `curry` and make it pass the tests.
```rust
# trait Curried<A, B>: FnOnce(A) -> Self::Inner {
# type C;
# type Inner: FnOnce(B) -> Self::C;
# }
# impl<A, B, C, Inner: FnOnce(B) -> C, F: FnOnce(A) -> Inner> Curried<A, B> for F {
# type C = C;
# type Inner = Inner;
# }
# fn curry<A, B, C>(f: impl FnOnce(A, B) -> C) -> impl Curried<A, B, C = C> { |a| |b| f(a, b) }
assert_eq!(curry(|a, b| a * b)(3)(5), 15);
let mut x = 0;
curry(|x: &mut i32, y| *x = y)(&mut x)(5);
assert_eq!(x, 5);
```
Try solving it in the playground:
```rust,editable,compile_fail
fn main() {
assert_eq!(curry(|a, b| a * b)(3)(5), 15);
let mut x = 0;
curry(|x: &mut i32, y| *x = y)(&mut x)(5);
assert_eq!(x, 5);
}
```

View File

@ -5,10 +5,10 @@ Rows are ordered based on estimated difficulty.
| lifetimes\[-adjacent\] | `trait`s | `struct`s |
|------------------------|--------------------|---------------------|
| [explicit lifetimes] | [`Fn`?] | [`Duration`] |
| [from reference] | [`async` `Fn`] | [All Errors?] |
| [extracting lifetimes] | [composition] | [`RcChars`] |
| [`'static` return] | [exclusive traits] | [`AnyStr`] |
| | [merging traits] | [`Result` ordering] |
| [from reference] | [`curry`] | [All Errors?] |
| [extracting lifetimes] | [`async` `Fn`] | [`RcChars`] |
| [`'static` return] | [exclusive traits] | [`Result` ordering] |
| [composition] | [merging traits] | [`AnyStr`] |
p.s. most of the exercises are actually about `trait`s, this categorisation isn't strict.
@ -26,3 +26,4 @@ p.s. most of the exercises are actually about `trait`s, this categorisation isn'
[`async` `Fn`]: ./exercises/async_fn.md
[`'static` return]: ./exercises/get_functions.md
[`Result` ordering]: ./exercises/err_err.md
[`curry`]: ./exercises/curry.md