Skip to content

Commit

Permalink
calculating with functions
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheyca committed Aug 30, 2024
1 parent 49aed88 commit 9ec8ad0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Async Fn](./exercises/async_fn.md)
- [Get Functions](./exercises/get_functions.md)
- [Linked](./exercises/linked.md)
- [Calculating with Functions (based on Codewars)](./exercises/cwf.md)
- [Chapter 2](./chapter_2.md)
- [AnyStr](./exercises/anystr.md)
- [Mode](./exercises/mode.md)
Expand Down
34 changes: 34 additions & 0 deletions src/exercises/cwf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Calculating with Functions (based on Codewars)

Original: [Calculating with Functions](https://www.codewars.com/kata/525f3eda17c7cd9f9e000b39)

As you might know, on *stable* Rust you can't have variable number of arguments passed to a
function. However we can alter the exercise to work anyway.

```rust
# trait Value { fn value(self) -> i32; }
# trait Op { fn apply(self, value: i32) -> i32; }
# struct Identity;
# impl Op for Identity { fn apply(self, value: i32) -> i32 { value } }
# impl<F: FnOnce(i32) -> i32> Op for F { fn apply(self, value: i32) -> i32 { self(value) } }
# impl<F: FnOnce(Identity) -> i32> Value for F { fn value(self) -> i32 { self(Identity) } }
# impl Value for i32 { fn value(self) -> i32 { self } }
# fn zero(f: impl Op) -> i32 { f.apply(0) }
# fn one(f: impl Op) -> i32 { f.apply(1) }
# fn two(f: impl Op) -> i32 { f.apply(2) }
# fn three(f: impl Op) -> i32 { f.apply(3) }
# fn four(f: impl Op) -> i32 { f.apply(4) }
# fn five(f: impl Op) -> i32 { f.apply(5) }
# fn six(f: impl Op) -> i32 { f.apply(6) }
# fn seven(f: impl Op) -> i32 { f.apply(7) }
# fn eight(f: impl Op) -> i32 { f.apply(8) }
# fn nine(f: impl Op) -> i32 { f.apply(9) }
# fn plus(x: impl Value) -> impl FnOnce(i32) -> i32 { |y| y + x.value() }
# fn minus(x: impl Value) -> impl FnOnce(i32) -> i32 { |y| y - x.value() }
# fn times(x: impl Value) -> impl FnOnce(i32) -> i32 { |y| y * x.value() }
# fn divided_by(x: impl Value) -> impl FnOnce(i32) -> i32 { |y| y / x.value() }
assert_eq!(seven(times(five)), 35);
assert_eq!(four(plus(nine)), 13);
assert_eq!(eight(minus(three)), 5);
assert_eq!(six(divided_by(two)), 3);
```

0 comments on commit 9ec8ad0

Please sign in to comment.