Chapter 1 excercises

This commit is contained in:
Vladan Popovic 2019-08-11 13:50:31 +02:00
parent b1145921cc
commit c6c48de95f
4 changed files with 36 additions and 0 deletions

2
chapter-1/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
**/*.rs.bk

6
chapter-1/Cargo.lock generated Normal file
View File

@ -0,0 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "chapter-1"
version = "0.1.0"

9
chapter-1/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "chapter-1"
version = "0.1.0"
authors = ["Vladan Popovic <vladanovic@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

19
chapter-1/src/main.rs Normal file
View File

@ -0,0 +1,19 @@
fn id<T>(x: T) -> T {
x
}
fn compose<A, B, C, F, G>(f: F, g: G) -> impl Fn(A) -> C
where
F: Fn(A) -> B,
G: Fn(B) -> C,
{
move |x: A| g(f(x))
}
fn inc(x: u32) -> u32 {
x + 1
}
fn main() {
assert!(compose(id, inc)(2) == compose(inc, id)(2));
}