Skip to content

Commit b94ccd9

Browse files
committed
Add more methods to MaybeNanExt
1 parent e97f63c commit b94ccd9

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/maybe_nan/mod.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,24 @@ where
250250
A: 'a,
251251
F: FnMut(B, &'a A::NotNan) -> B;
252252

253+
/// Visit each non-NaN element in the array by calling `f` on each element.
254+
///
255+
/// Elements are visited in arbitrary order.
256+
fn visit_skipnan<'a, F>(&'a self, f: F)
257+
where
258+
A: 'a,
259+
F: FnMut(&'a A::NotNan);
260+
261+
/// Fold non-NaN values along an axis.
262+
///
263+
/// Combine the non-NaN elements of each subview with the previous using
264+
/// the fold function and initial value init.
265+
fn fold_axis_skipnan<B, F>(&self, axis: Axis, init: B, fold: F) -> Array<B, D::Smaller>
266+
where
267+
D: RemoveAxis,
268+
F: FnMut(&B, &A::NotNan) -> B,
269+
B: Clone;
270+
253271
/// Reduce the values along an axis into just one value, producing a new
254272
/// array with one less dimension.
255273
///
@@ -293,6 +311,33 @@ where
293311
})
294312
}
295313

314+
fn visit_skipnan<'a, F>(&'a self, mut f: F)
315+
where
316+
A: 'a,
317+
F: FnMut(&'a A::NotNan),
318+
{
319+
self.visit(|elem| {
320+
if let Some(not_nan) = elem.try_as_not_nan() {
321+
f(not_nan)
322+
}
323+
})
324+
}
325+
326+
fn fold_axis_skipnan<B, F>(&self, axis: Axis, init: B, mut fold: F) -> Array<B, D::Smaller>
327+
where
328+
D: RemoveAxis,
329+
F: FnMut(&B, &A::NotNan) -> B,
330+
B: Clone,
331+
{
332+
self.fold_axis(axis, init, |acc, elem| {
333+
if let Some(not_nan) = elem.try_as_not_nan() {
334+
fold(acc, not_nan)
335+
} else {
336+
acc.clone()
337+
}
338+
})
339+
}
340+
296341
fn map_axis_skipnan_mut<'a, B, F>(
297342
&'a mut self,
298343
axis: Axis,

0 commit comments

Comments
 (0)