Skip to content

Commit 4d09f58

Browse files
committed
Add mapv_into and modify docs
1 parent 0ef1bd4 commit 4d09f58

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/impl_methods.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,9 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
10951095
init
10961096
}
10971097

1098-
/// Create a new array with the results of applying `f` on a reference
1099-
/// to each element.
1098+
/// Call `f` by reference on each element and create a new array
1099+
/// with the results.
1100+
///
11001101
/// Elements are visited in arbitrary order.
11011102
///
11021103
/// Return an array with the same shape as *self*.
@@ -1130,8 +1131,9 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
11301131
}
11311132
}
11321133

1133-
/// Create a new array with the results of applying `f` on the value of
1134-
/// each element.
1134+
/// Call `f` by value on each element and create a new array
1135+
/// with the new values.
1136+
///
11351137
/// Elements are visited in arbitrary order.
11361138
///
11371139
/// Return an array with the same shape as *self*.
@@ -1141,7 +1143,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
11411143
///
11421144
/// let a = arr2(&[[ 0., 1.],
11431145
/// [-1., 2.]]);
1144-
/// assert!(
1146+
/// assert_eq!(
11451147
/// a.mapv(f32::abs) == arr2(&[[0., 1.],
11461148
/// [1., 2.]])
11471149
/// );
@@ -1153,8 +1155,21 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
11531155
self.map(move |x| f(x.clone()))
11541156
}
11551157

1156-
/// Modify the array in place by calling `f` with a mutable reference
1157-
/// to each element.
1158+
/// Call `f` by value on each element, update the array with new values
1159+
/// and return it.
1160+
///
1161+
/// Elements are visited in arbitrary order.
1162+
pub fn mapv_into<F>(mut self, f: F) -> Self
1163+
where S: DataMut,
1164+
F: FnMut(A) -> A,
1165+
A: Clone,
1166+
{
1167+
self.applyv(f);
1168+
self
1169+
}
1170+
1171+
/// Modify in place by calling `f` by mutable reference on each element.
1172+
///
11581173
/// Elements are visited in arbitrary order.
11591174
pub fn apply<F>(&mut self, f: F)
11601175
where S: DataMut,
@@ -1163,8 +1178,9 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
11631178
self.unordered_foreach_mut(f);
11641179
}
11651180

1166-
/// Modify the array in place by calling a function `f` that maps
1167-
/// each element by value (from `A` to `A`).
1181+
/// Modify in place by calling `f` by value on each element. The
1182+
/// array is updated with the new values.
1183+
///
11681184
/// Elements are visited in arbitrary order.
11691185
///
11701186
/// ```
@@ -1173,6 +1189,10 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
11731189
/// let mut a = arr2(&[[ 0., 1.],
11741190
/// [-1., 2.]]);
11751191
/// a.applyv(f32::exp);
1192+
/// assert!(
1193+
/// a.allclose(&arr2(&[[1.00000, 2.71828],
1194+
/// [0.36788, 7.38906]]), 1e-5)
1195+
/// );
11761196
/// ```
11771197
pub fn applyv<F>(&mut self, mut f: F)
11781198
where S: DataMut,

0 commit comments

Comments
 (0)