diff --git a/src/SUMMARY.md b/src/SUMMARY.md index ced0b3d..d47311f 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -21,5 +21,6 @@ - [Covariance]() - [Option, Vec and BoxFuture<'a>]() - [Err Err](./exercises/err_err.md) + - [Curry](./exercises/curry.md) [Topics (Spoilers)](./topics.md) diff --git a/src/exercises/curry.md b/src/exercises/curry.md new file mode 100644 index 0000000..397de94 --- /dev/null +++ b/src/exercises/curry.md @@ -0,0 +1,30 @@ +# Currying + +Implement `curry` and make it pass the tests. + +```rust +# trait Curried: FnOnce(A) -> Self::Inner { +# type C; +# type Inner: FnOnce(B) -> Self::C; +# } +# impl C, F: FnOnce(A) -> Inner> Curried for F { +# type C = C; +# type Inner = Inner; +# } +# fn curry(f: impl FnOnce(A, B) -> C) -> impl Curried { |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); +} +``` diff --git a/src/topics.md b/src/topics.md index b118e15..4784b2f 100644 --- a/src/topics.md +++ b/src/topics.md @@ -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