rust - Is there a fast way to check a Vec for an error result? -
i'm using rayon iterate on vector, producing vec
of results:
let coordinates = &[[38.5, -120.2], [40.7, -120.95], [430.252, -126.453]] let mut res = vec![]; coordinates .par_iter() .map(|pair| { match (check(&pair[0]), check(&pair[1])) { (ok(v1), ok(v2)) => ok([v1, v2]), (err(v), _) => err(v), (_, err(v)) => err(v), } }) .collect_into(&mut res);
i'd check res
error values, convert them string
, return them using try!()
this works, it's slow , inefficient, considering i'm allocating new vector aggregate results or pull out error:
let errcheck: result<vec<_>, f64> = res.iter().map(|elem| *elem).collect(); try!(errcheck.map_err(|e| format!("error: {}", e).to_string()));
this problem appears rayon-specific; if use .iter()
, can collect directly errcheck
using collect()
, map_err()
in match arms, can't seem using par_iter()
.
is there better way it?
if need items not fulfil conditions, there filter()
:
let bounds1 = (-90.0, 90.0); let bounds2 = (-180.0, 180.0); let (xmin, xmax) = bounds1; let (ymin, ymax) = bounds2; coordinates.par_iter().filter(|pair| { let x = pair[0]; let y = pair[1]; !((xmin <= x) && (x <= xmax) && (ymin <= y) && (y <= ymax)) }).for_each(|pair| { println!("bad pair: {} {}", pair[0], pair[1]); });
in other words, map right operation in first place.
Comments
Post a Comment