parent
0c65a982a1
commit
dcd9e832e2
@ -21,5 +21,6 @@
|
|||||||
- [Covariance]()
|
- [Covariance]()
|
||||||
- [Option, Vec and BoxFuture<'a>]()
|
- [Option, Vec and BoxFuture<'a>]()
|
||||||
- [Err Err](./exercises/err_err.md)
|
- [Err Err](./exercises/err_err.md)
|
||||||
|
- [Curry](./exercises/curry.md)
|
||||||
|
|
||||||
[Topics (Spoilers)](./topics.md)
|
[Topics (Spoilers)](./topics.md)
|
||||||
|
30
src/exercises/curry.md
Normal file
30
src/exercises/curry.md
Normal 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);
|
||||||
|
}
|
||||||
|
```
|
@ -5,10 +5,10 @@ Rows are ordered based on estimated difficulty.
|
|||||||
| lifetimes\[-adjacent\] | `trait`s | `struct`s |
|
| lifetimes\[-adjacent\] | `trait`s | `struct`s |
|
||||||
|------------------------|--------------------|---------------------|
|
|------------------------|--------------------|---------------------|
|
||||||
| [explicit lifetimes] | [`Fn`?] | [`Duration`] |
|
| [explicit lifetimes] | [`Fn`?] | [`Duration`] |
|
||||||
| [from reference] | [`async` `Fn`] | [All Errors?] |
|
| [from reference] | [`curry`] | [All Errors?] |
|
||||||
| [extracting lifetimes] | [composition] | [`RcChars`] |
|
| [extracting lifetimes] | [`async` `Fn`] | [`RcChars`] |
|
||||||
| [`'static` return] | [exclusive traits] | [`AnyStr`] |
|
| [`'static` return] | [exclusive traits] | [`Result` ordering] |
|
||||||
| | [merging traits] | [`Result` ordering] |
|
| [composition] | [merging traits] | [`AnyStr`] |
|
||||||
|
|
||||||
p.s. most of the exercises are actually about `trait`s, this categorisation isn't strict.
|
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
|
[`async` `Fn`]: ./exercises/async_fn.md
|
||||||
[`'static` return]: ./exercises/get_functions.md
|
[`'static` return]: ./exercises/get_functions.md
|
||||||
[`Result` ordering]: ./exercises/err_err.md
|
[`Result` ordering]: ./exercises/err_err.md
|
||||||
|
[`curry`]: ./exercises/curry.md
|
||||||
|
Loading…
Reference in New Issue
Block a user