Skip to content

Commit bca2dc9

Browse files
committed
Make GridBuilder::from_array return Result
This is nice because it doesn't lose information. (Returning an `Option` combines the two error variants into a single case.)
1 parent 29f56f3 commit bca2dc9

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/histogram/grid.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::bins::Bins;
2+
use super::errors::BinsBuildError;
23
use super::strategies::BinsBuildingStrategy;
34
use itertools::izip;
45
use ndarray::{ArrayBase, Axis, Data, Ix1, Ix2};
@@ -169,23 +170,20 @@ where
169170
/// it returns a `GridBuilder` instance that has learned the required parameter
170171
/// to build a [`Grid`] according to the specified [`strategy`].
171172
///
172-
/// It returns `None` if it is not possible to build a [`Grid`] given
173+
/// It returns `Err` if it is not possible to build a [`Grid`] given
173174
/// the observed data according to the chosen [`strategy`].
174175
///
175176
/// [`Grid`]: struct.Grid.html
176177
/// [`strategy`]: strategies/index.html
177-
pub fn from_array<S>(array: &ArrayBase<S, Ix2>) -> Option<Self>
178+
pub fn from_array<S>(array: &ArrayBase<S, Ix2>) -> Result<Self, BinsBuildError>
178179
where
179180
S: Data<Elem = A>,
180181
{
181-
let bin_builders: Option<Vec<B>> = array
182+
let bin_builders = array
182183
.axis_iter(Axis(1))
183-
.map(|data| match B::from_array(&data) {
184-
Ok(builder) => Some(builder),
185-
_ => None,
186-
})
187-
.collect();
188-
bin_builders.map(|b| Self { bin_builders: b })
184+
.map(|data| B::from_array(&data))
185+
.collect::<Result<Vec<B>, BinsBuildError>>()?;
186+
Ok(Self { bin_builders })
189187
}
190188

191189
/// Returns a [`Grid`] instance, built accordingly to the specified [`strategy`]

0 commit comments

Comments
 (0)