Skip to content

Commit

Permalink
curry
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheyca committed Dec 1, 2023
1 parent 0c65a98 commit dcd9e83
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 30 additions & 0 deletions src/exercises/curry.md
Original file line number Diff line number Diff line change
@@ -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);
}
```
9 changes: 5 additions & 4 deletions src/topics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

0 comments on commit dcd9e83

Please sign in to comment.