diff --git a/arithmetic_progression.rs b/arithmetic_progression.rs index eb776ad..17d2474 100644 --- a/arithmetic_progression.rs +++ b/arithmetic_progression.rs @@ -1,17 +1,56 @@ // https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=9729edf14743236bf3e936225c8d7880 +#![feature(iter_map_windows)] use itertools::Itertools; -use std::{fmt::Debug, ops::Sub}; +use core::{fmt::Debug, ops::Sub, cmp::Ordering}; fn is_arithmetic_progression( - nums: impl IntoIterator>, + nums: impl IntoIterator>, ) -> bool { nums.into_iter() - .sorted_by(|a, b| a.partial_cmp(b).unwrap()) + .sorted_by(TotalOrd::total_cmp) .map_windows(|&[a, b]| b - a) .all_equal() } +trait TotalOrd { + fn total_cmp(&self, rhs: &Self) -> Ordering; +} + +impl TotalOrd for &T { + fn total_cmp(&self, rhs: &Self) -> Ordering { + (**self).total_cmp(*rhs) + } +} + +macro_rules! impl_float { + ($($ty:ty)+) => { + $( + impl TotalOrd for $ty { + fn total_cmp(&self, rhs: &Self) -> Ordering { + <$ty>::total_cmp(self, rhs) + } + } + )+ + } +} + +impl_float!(f32 f64); + +macro_rules! impl_int { + ($($ty:ty)+) => { + $( + impl TotalOrd for $ty { + fn total_cmp(&self, rhs: &Self) -> Ordering { + <$ty>::cmp(self, rhs) + } + } + )+ + } +} + +impl_int!(i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize); + fn print_result(example: usize, nums: impl Debug, result: bool) { print!("Example {example}:\nInput: {nums:?}\nOutput: {result}\n"); }