Rename sheet folders to assure proper sorting

This commit is contained in:
Lukas Kalbertodt
2017-02-16 15:12:56 +01:00
parent aaf0332117
commit b3664c6ffd
74 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,6 @@
[package]
name = "sol1"
version = "0.1.0"
authors = ["Lukas Kalbertodt <lukas.kalbertodt@gmail.com>"]
[dependencies]

View File

@ -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<T>(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<T, U>(a: T, b: U)
-> (<T as Add<U>>::Output, <T as Mul<U>>::Output)
where T: Add<U> + Mul<U> + Clone,
U: Clone
{
(a.clone() + b.clone(), a * b)
}
/// Extension trait for simple conversion from `bool` to `Option<T>`
pub trait BoolOptionExt {
/// If `self` is `true`, `Some(value)` is returned, `None` otherwise.
fn into_option<T>(self, value: T) -> Option<T>;
}
impl BoolOptionExt for bool {
fn into_option<T>(self, value: T) -> Option<T> {
match self {
true => Some(value),
false => None,
}
}
}

View File

@ -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));
}