Skip to content

Commit cb544a0

Browse files
authored
Merge pull request #907 from jturner314/rename-visit-to-for_each
Rename .visit() to .for_each()
2 parents 480f45b + 73b4f84 commit cb544a0

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

src/doc/ndarray_for_numpy_users/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@
282282
//! Note that [`.mapv()`][.mapv()] has corresponding methods [`.map()`][.map()],
283283
//! [`.mapv_into()`][.mapv_into()], [`.map_inplace()`][.map_inplace()], and
284284
//! [`.mapv_inplace()`][.mapv_inplace()]. Also look at [`.fold()`][.fold()],
285-
//! [`.visit()`][.visit()], [`.fold_axis()`][.fold_axis()], and
285+
//! [`.for_each()`][.for_each()], [`.fold_axis()`][.fold_axis()], and
286286
//! [`.map_axis()`][.map_axis()].
287287
//!
288288
//! <table>
@@ -648,7 +648,7 @@
648648
//! [.sum_axis()]: ../../struct.ArrayBase.html#method.sum_axis
649649
//! [.t()]: ../../struct.ArrayBase.html#method.t
650650
//! [vec-* dot]: ../../struct.ArrayBase.html#method.dot
651-
//! [.visit()]: ../../struct.ArrayBase.html#method.visit
651+
//! [.for_each()]: ../../struct.ArrayBase.html#method.for_each
652652
//! [::zeros()]: ../../struct.ArrayBase.html#method.zeros
653653
//! [Zip]: ../../struct.Zip.html
654654

src/impl_methods.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,17 +2238,30 @@ where
22382238
self.unordered_foreach_mut(move |x| *x = f(x.clone()));
22392239
}
22402240

2241+
/// Call `f` for each element in the array.
2242+
///
2243+
/// Elements are visited in arbitrary order.
2244+
pub fn for_each<'a, F>(&'a self, mut f: F)
2245+
where
2246+
F: FnMut(&'a A),
2247+
A: 'a,
2248+
S: Data,
2249+
{
2250+
self.fold((), move |(), elt| f(elt))
2251+
}
2252+
22412253
/// Visit each element in the array by calling `f` by reference
22422254
/// on each element.
22432255
///
22442256
/// Elements are visited in arbitrary order.
2245-
pub fn visit<'a, F>(&'a self, mut f: F)
2257+
#[deprecated(note="Renamed to .for_each()", since="0.15.0")]
2258+
pub fn visit<'a, F>(&'a self, f: F)
22462259
where
22472260
F: FnMut(&'a A),
22482261
A: 'a,
22492262
S: Data,
22502263
{
2251-
self.fold((), move |(), elt| f(elt))
2264+
self.for_each(f)
22522265
}
22532266

22542267
/// Fold along an axis.

src/numeric/impl_numeric.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ where
168168
let mut mean = A::zero();
169169
let mut sum_sq = A::zero();
170170
let mut i = 0;
171-
self.visit(|&x| {
171+
self.for_each(|&x| {
172172
let count = A::from_usize(i + 1).expect("Converting index to `A` must not fail.");
173173
let delta = x - mean;
174174
mean = mean + delta / count;

tests/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ fn zero_axes() {
925925
}
926926
a.map(|_| panic!());
927927
a.map_inplace(|_| panic!());
928-
a.visit(|_| panic!());
928+
a.for_each(|_| panic!());
929929
println!("{:?}", a);
930930
let b = arr2::<f32, _>(&[[], [], [], []]);
931931
println!("{:?}\n{:?}", b.shape(), b);

0 commit comments

Comments
 (0)