Skip to content
Snippets Groups Projects
Select Git revision
  • ce33884d8f2e8b96565a1b6bfbcdd2c6b5d378e6
  • main default protected
  • jw_sonar
  • v6.0.0 protected
  • bedran_exercise-list
  • ask-user-to-delete-exercises-on-duplicates
  • update-dependencies
  • jw_sonar_backup
  • add_route_assignments
  • 6.0.0-dev
  • 5.0.1
  • 5.0.0
  • 4.1.0
  • 4.0.0
  • 3.5.3
  • 3.5.2
  • 3.5.1
  • 3.5.0
  • 3.4.2
  • 3.4.1
  • 3.4.0
  • 3.3.0
  • 3.2.0
  • 3.1.3
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
  • 3.0.0
29 results

dataSources

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