From c6c48de95fe992f5a9fc633e2ab8a757506b749a Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Sun, 11 Aug 2019 13:50:31 +0200 Subject: [PATCH] Chapter 1 excercises --- chapter-1/.gitignore | 2 ++ chapter-1/Cargo.lock | 6 ++++++ chapter-1/Cargo.toml | 9 +++++++++ chapter-1/src/main.rs | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 chapter-1/.gitignore create mode 100644 chapter-1/Cargo.lock create mode 100644 chapter-1/Cargo.toml create mode 100644 chapter-1/src/main.rs diff --git a/chapter-1/.gitignore b/chapter-1/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/chapter-1/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/chapter-1/Cargo.lock b/chapter-1/Cargo.lock new file mode 100644 index 0000000..c45cb25 --- /dev/null +++ b/chapter-1/Cargo.lock @@ -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" + diff --git a/chapter-1/Cargo.toml b/chapter-1/Cargo.toml new file mode 100644 index 0000000..af4ad80 --- /dev/null +++ b/chapter-1/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "chapter-1" +version = "0.1.0" +authors = ["Vladan Popovic "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/chapter-1/src/main.rs b/chapter-1/src/main.rs new file mode 100644 index 0000000..91855a9 --- /dev/null +++ b/chapter-1/src/main.rs @@ -0,0 +1,19 @@ +fn id(x: T) -> T { + x +} + +fn compose(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)); +}