Skip to content
Snippets Groups Projects
Select Git revision
  • a0b95b718f86b7d3ea34ecdd6812c4ed901ae3b7
  • master default protected
2 results

sandbox.o

Blame
  • binary_operator.rs 1.71 KiB
    // ANCHOR: binary_operator
    pub type BinaryOperator<T> = fn(T, T) -> T;
    // ANCHOR_END: binary_operator
    
    /// Returns a closure that computes the minimum
    /// between two elements of type T.
    /// # Example
    ///
    /// ```
    /// # use part08::binary_operator::{minimum_operator};
    /// # fn main() {
    /// let f = minimum_operator();
    /// assert!(f(1,2) == 1);
    /// # }
    /// ```
    // ANCHOR: minimum_operator
    pub fn minimum_operator<T: PartialOrd>() -> BinaryOperator<T> {
        |x: T, y: T| if x <= y { x } else { y }
    }
    // ANCHOR_END: minimum_operator
    
    /// Returns a closure that computes the maximum
    /// between two elements of type T.
    /// # Example
    ///
    /// ```
    /// # use part08::binary_operator::{maximum_operator};
    /// # fn main() {
    /// let f = maximum_operator();
    /// assert!(f(1,2) == 2);
    /// # }
    /// ```
    // ANCHOR: maximum_operator
    pub fn maximum_operator<T: PartialOrd>() -> BinaryOperator<T> {
        |x: T, y: T| if x >= y { x } else { y }
    }
    // ANCHOR_END: maximum_operator
    
    /// Returns a closure that computes the sum
    /// of two elements of type T.
    /// # Example
    ///
    /// ```
    /// # use part08::binary_operator::{sum_operator};
    /// # fn main() {
    /// let f = sum_operator();
    /// assert!(f(1,2) == 3);
    /// # }
    /// ```
    // ANCHOR: sum_operator
    pub fn sum_operator<T: std::ops::Add<Output = T>>() -> BinaryOperator<T> {
        |x: T, y: T| x + y
    }
    // ANCHOR_END: sum_operator
    
    /// Returns a closure that computes the product
    /// of two elements of type T.
    /// # Example
    ///
    /// ```
    /// # use part08::binary_operator::{mul_operator};
    /// # fn main() {
    /// let f = mul_operator();
    /// assert!(f(1,2) == 2);
    /// # }
    /// ```
    // ANCHOR: mul_operator
    pub fn mul_operator<T: std::ops::Mul<Output = T>>() -> BinaryOperator<T> {
        |x: T, y: T| x * y
    }
    // ANCHOR_END: mul_operator