Good ways to deal with operations that return Result when using Ndarray

71 views Asked by At

Recently I've started using ndarray to handle my multi-dimentional arrays when using Rust, and I love a lot of it's convenience features, if I could just figure out one problem, it would be perfect.

Normally, when you collect an iterator over set a Results (e.g. you just did a map operation that was fallible), you can tell rust that you would like to collect it as a Result<Vec<_>, _>, instead of a Vec<Result<, _>>. This is super helpful, but as far as I can tell Ndarray's producers don't have a similar feature? What I'd like to know is how can I efficiently deal with Results when using Ndarray's Producers/when using Zip to operate over a ndarray.

All I can think to do is to either turn the ndarray into a normal iterator, which I don't want to do because the dimentionality is lost (e.g. an Array3 turns into a Vec), do some kind of second iteration after the first to find any Err variants, which seems inefficient, or just use unwrap everywhere...

I'd really appreciate any other perspectives on using ndarray, or other ways to manage multi-dimensional arrays in rust.

code-example:

let thingy: Array3 = whatever;

let output = Zip::from(thingy).and(another_thingy).map_collect(|thing1, thing2| {
    function_that_returns_result(thing1, thing2)
});

//output ends up being an Array3 of Results, where I would like an idiomatic way for it to be a Result containing an Array3
0

There are 0 answers