Hacker News new | past | comments | ask | show | jobs | submit login

Why a macro? Why not just define that comparator as a function?

    fn value_nans_last<T: Float>(a: &T, b: &T) -> Ordering {
      match (a, b) {
        (x, y) if x.is_nan() && y.is_nan() => Ordering::Equal,
        (x, _) if x.is_nan() => Ordering::Greater,
        (_, y) if y.is_nan() => Ordering::Less,
        (_, _) => a.partial_cmp(b).unwrap()
      }
    }

    xs.sort_by(value_nans_last);



I haven't done sorting, so no knowledge of sort interface in Rust, but maybe so it works across different sortable collections?


There's no need to use a macro: you can abstract over different kinds of sortable collections using a trait. (That said, there's no real reason to generalize IMO: by far the most common case is sorting slices.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: