From 08a4b2fccc80784f4b245adccd0185d6c26789a6 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Mon, 5 Dec 2016 17:57:47 +0100 Subject: [PATCH] Add solutions for sheet6 --- aufgaben/sheet6/sol1/Cargo.toml | 6 ++++ aufgaben/sheet6/sol1/src/lib.rs | 52 +++++++++++++++++++++++++++ aufgaben/sheet6/sol1/src/tests.rs | 28 +++++++++++++++ aufgaben/sheet6/sol2/Cargo.toml | 7 ++++ aufgaben/sheet6/sol2/src/lib.rs | 59 +++++++++++++++++++++++++++++++ aufgaben/sheet6/sol2/src/tests.rs | 18 ++++++++++ aufgaben/sheet6/sol3/swagger.rs | 27 ++++++++++++++ 7 files changed, 197 insertions(+) create mode 100755 aufgaben/sheet6/sol1/Cargo.toml create mode 100755 aufgaben/sheet6/sol1/src/lib.rs create mode 100755 aufgaben/sheet6/sol1/src/tests.rs create mode 100755 aufgaben/sheet6/sol2/Cargo.toml create mode 100755 aufgaben/sheet6/sol2/src/lib.rs create mode 100755 aufgaben/sheet6/sol2/src/tests.rs create mode 100755 aufgaben/sheet6/sol3/swagger.rs diff --git a/aufgaben/sheet6/sol1/Cargo.toml b/aufgaben/sheet6/sol1/Cargo.toml new file mode 100755 index 0000000..2afa626 --- /dev/null +++ b/aufgaben/sheet6/sol1/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "sol1" +version = "0.1.0" +authors = ["Lukas Kalbertodt "] + +[dependencies] diff --git a/aufgaben/sheet6/sol1/src/lib.rs b/aufgaben/sheet6/sol1/src/lib.rs new file mode 100755 index 0000000..0fc1782 --- /dev/null +++ b/aufgaben/sheet6/sol1/src/lib.rs @@ -0,0 +1,52 @@ +use std::ops::{Add, Mul}; + +#[cfg(test)] +mod tests; + + +/// Clamps a value into a given range. This function returns the value closest +/// to `value` which lies in between `min` and `max`. +/// +/// *Note*: it's not clear whether `PartialOrd` or `Ord` is the correct bound +/// here. With `PartialEq`, some results may look strange to some. +/// `clamp(NaN, 0.0, 5.0)` would return `NaN` for example. `NaN` as min or max +/// wouldn't do anything. +pub fn clamp(value: T, min: T, max: T) -> T + where T: PartialOrd +{ + // This is a small little trick. We want to avoid using if-else here, so + // we match the unit value `()` (void) and use the match guards. + match () { + () if value < min => min, + () if value > max => max, + _ => value, + } +} + + +/// Returns the sum and the product of the two given parameters. +/// +/// *Note*: Either a Clone or Copy bound is necessary. Clone was choosen here, +/// because it's more general. +pub fn sum_product(a: T, b: U) + -> (>::Output, >::Output) + where T: Add + Mul + Clone, + U: Clone +{ + (a.clone() + b.clone(), a * b) +} + +/// Extension trait for simple conversion from `bool` to `Option` +pub trait BoolOptionExt { + /// If `self` is `true`, `Some(value)` is returned, `None` otherwise. + fn into_option(self, value: T) -> Option; +} + +impl BoolOptionExt for bool { + fn into_option(self, value: T) -> Option { + match self { + true => Some(value), + false => None, + } + } +} diff --git a/aufgaben/sheet6/sol1/src/tests.rs b/aufgaben/sheet6/sol1/src/tests.rs new file mode 100755 index 0000000..abf1ccb --- /dev/null +++ b/aufgaben/sheet6/sol1/src/tests.rs @@ -0,0 +1,28 @@ +#[test] +fn clamp() { + use clamp; + + assert_eq!(clamp(3, 5, 10), 5); + assert_eq!(clamp(6, 5, 10), 6); + assert_eq!(clamp(11, 5, 10), 10); + + assert_eq!(clamp(3.0, 5.0, 10.0), 5.0); + assert_eq!(clamp(6.0, 5.0, 10.0), 6.0); + assert_eq!(clamp(11.0, 5.0, 10.0), 10.0); +} + +#[test] +fn sum_product() { + use sum_product; + + assert_eq!(sum_product(3, 4), (7, 12)); + assert_eq!(sum_product(3.0, 4.0), (7.0, 12.0)); +} + +#[test] +fn bool_option() { + use BoolOptionExt; + + assert_eq!(false.into_option(3), None); + assert_eq!( true.into_option(3), Some(3)); +} diff --git a/aufgaben/sheet6/sol2/Cargo.toml b/aufgaben/sheet6/sol2/Cargo.toml new file mode 100755 index 0000000..7b9b86b --- /dev/null +++ b/aufgaben/sheet6/sol2/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "sol2" +version = "0.1.0" +authors = ["Lukas Kalbertodt "] + +[dependencies] +num-traits = "0.1" diff --git a/aufgaben/sheet6/sol2/src/lib.rs b/aufgaben/sheet6/sol2/src/lib.rs new file mode 100755 index 0000000..5c629e9 --- /dev/null +++ b/aufgaben/sheet6/sol2/src/lib.rs @@ -0,0 +1,59 @@ +extern crate num_traits; + +use num_traits::{Zero, One}; +use std::ops; + +#[cfg(test)] +mod tests; + +#[derive(Debug, PartialEq, Clone, Copy)] +pub struct Vector2 { + pub x: T, + pub y: T, +} + +impl Vector2 { + pub fn new(x: T, y: T) -> Self { + Vector2 { + x: x, + y: y, + } + } +} + +impl Vector2 { + pub fn origin() -> Self { + Self::new(T::zero(), T::zero()) + } +} + +impl Vector2 { + pub fn unit_x() -> Self { + Self::new(T::one(), T::zero()) + } + + pub fn unit_y() -> Self { + Self::new(T::zero(), T::one()) + } +} + +impl ops::Add> for Vector2 + where T: ops::Add +{ + type Output = Vector2; + + fn add(self, rhs: Vector2) -> Self::Output { + Vector2::new(self.x + rhs.x, self.y + rhs.y) + } +} + +impl ops::Mul for Vector2 + where T: ops::Mul, + U: Clone +{ + type Output = Vector2; + + fn mul(self, rhs: U) -> Self::Output { + Vector2::new(self.x * rhs.clone(), self.y * rhs) + } +} diff --git a/aufgaben/sheet6/sol2/src/tests.rs b/aufgaben/sheet6/sol2/src/tests.rs new file mode 100755 index 0000000..2c0c854 --- /dev/null +++ b/aufgaben/sheet6/sol2/src/tests.rs @@ -0,0 +1,18 @@ +use Vector2; + +#[test] +fn constructors() { + assert_eq!(Vector2::new(27, 42), Vector2 { x: 27, y: 42 }); + assert_eq!(Vector2::origin(), Vector2 { x: 0, y: 0 }); + assert_eq!(Vector2::unit_x(), Vector2 { x: 1, y: 0 }); + assert_eq!(Vector2::unit_y(), Vector2 { x: 0, y: 1 }); +} + +#[test] +fn operators() { + let a = Vector2::new(3, 7); + let b = Vector2::new(-2, 5); + + assert_eq!(a + b, Vector2::new(1, 12)); + assert_eq!(a * 2, Vector2::new(6, 14)); +} diff --git a/aufgaben/sheet6/sol3/swagger.rs b/aufgaben/sheet6/sol3/swagger.rs new file mode 100755 index 0000000..5bacccd --- /dev/null +++ b/aufgaben/sheet6/sol3/swagger.rs @@ -0,0 +1,27 @@ +use std::fmt; + +struct Swagger(pub T); + +impl fmt::Display for Swagger { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(f, "yolo {} swag", self.0) + } +} + +trait SwaggerExt: Sized { + fn with_swag(self) -> Swagger; +} + +impl SwaggerExt for T { + fn with_swag(self) -> Swagger { + Swagger(self) + } +} + +fn main() { + let pi = 3.14; + + println!("{}", pi); + println!("{}", Swagger(pi)); + println!("{}", pi.with_swag()); +}