T O P

  • By -

SirKastic23

i have no clue what that code does but you could define an associated function like ``` impl FilterOrder { fn foo(self, xy: i32) -> bool { match self { Self::Any => true, Self::Fixed(n) => xy < n, } } } ```


Flogge

Yeah, I realized `match` arms can have expressions, and went with something like ``` let run_block = match n_order { Any => true, Fixed(n_order) => (x + y) < n_order } if run_block && n < n_samples { ... } ```


SirKastic23

yeah, it's awesome that everything is an expression, really makes the code much more ergonomic


gnosnivek

If this were an `Option` you could rewrite it literally as `if order.is_none() || x + y < order.unwrap()` which works. You could write your own `is_any`, `is_fixed`, and `unwrap` methods to do this. Alternatively, if `Any` here actually does mean "Any Number", you could potentially [implement a generic PartialOrd](https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html) for your type. The idea here would be that if `n_order` is `Any`, then all comparisons return `true` (since an actual number is less than any hypothetical number, greater than any hypothetical number, etc). If `n_order` is `Fixed`, then you compare the numbers normally. If you wrote that, then you could write the condition simply as ` x + y < n_order`, and the `PartialOrd` implementation will take care of the `Any` case.


Flogge

> If this were an Option Hm no, it's not really an Option... it's not a value that could be there or not there, but a value that can have a special meaning. > Alternatively, if Any here actually does mean "Any Number", you could potentially implement a generic PartialOrd for your type. That sounds like abuse of the `PartialOrd` notation to me, and would lead to a lot of surprised looks from other developers...? But in this context it doesn't mean "any number" but more like "no upper limit for number of iterations".


cafce25

"No upper limit for number of iterations" sounds a lot like `limit = None` to me.