@@ -1095,8 +1095,9 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1095
1095
init
1096
1096
}
1097
1097
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
+ ///
1100
1101
/// Elements are visited in arbitrary order.
1101
1102
///
1102
1103
/// 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
1130
1131
}
1131
1132
}
1132
1133
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
+ ///
1135
1137
/// Elements are visited in arbitrary order.
1136
1138
///
1137
1139
/// 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
1141
1143
///
1142
1144
/// let a = arr2(&[[ 0., 1.],
1143
1145
/// [-1., 2.]]);
1144
- /// assert !(
1146
+ /// assert_eq !(
1145
1147
/// a.mapv(f32::abs) == arr2(&[[0., 1.],
1146
1148
/// [1., 2.]])
1147
1149
/// );
@@ -1153,8 +1155,21 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1153
1155
self . map ( move |x| f ( x. clone ( ) ) )
1154
1156
}
1155
1157
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
+ ///
1158
1173
/// Elements are visited in arbitrary order.
1159
1174
pub fn apply < F > ( & mut self , f : F )
1160
1175
where S : DataMut ,
@@ -1163,8 +1178,9 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1163
1178
self . unordered_foreach_mut ( f) ;
1164
1179
}
1165
1180
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
+ ///
1168
1184
/// Elements are visited in arbitrary order.
1169
1185
///
1170
1186
/// ```
@@ -1173,6 +1189,10 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1173
1189
/// let mut a = arr2(&[[ 0., 1.],
1174
1190
/// [-1., 2.]]);
1175
1191
/// a.applyv(f32::exp);
1192
+ /// assert!(
1193
+ /// a.allclose(&arr2(&[[1.00000, 2.71828],
1194
+ /// [0.36788, 7.38906]]), 1e-5)
1195
+ /// );
1176
1196
/// ```
1177
1197
pub fn applyv < F > ( & mut self , mut f : F )
1178
1198
where S : DataMut ,
0 commit comments