Skip to content

Commit 50573f8

Browse files
authored
Merge pull request #894 from rust-ndarray/zip-apply-renamed-to-for-each
Rename Zip::apply to for_each including other apply methods
2 parents c4482eb + 1f5b8d5 commit 50573f8

16 files changed

+153
-83
lines changed

examples/sort-axis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ where
112112
let perm_i = perm.indices[i];
113113
Zip::from(result.index_axis_mut(axis, perm_i))
114114
.and(self.index_axis(axis, i))
115-
.apply(|to, from| {
115+
.for_each(|to, from| {
116116
copy_nonoverlapping(from, to.as_mut_ptr(), 1);
117117
moved_elements += 1;
118118
});

examples/zip_many.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ fn main() {
4141
// Let's imagine we split to parallelize
4242
{
4343
let (x, y) = Zip::indexed(&mut a).split();
44-
x.apply(|(_, j), elt| {
44+
x.for_each(|(_, j), elt| {
4545
*elt = elt.powi(j as i32);
4646
});
4747

48-
y.apply(|(_, j), elt| {
48+
y.for_each(|(_, j), elt| {
4949
*elt = elt.powi(j as i32);
5050
});
5151
}

src/impl_methods.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,7 @@ where
19851985
let dim = self.raw_dim();
19861986
Zip::from(LanesMut::new(self.view_mut(), Axis(n - 1)))
19871987
.and(Lanes::new(rhs.broadcast_assume(dim), Axis(n - 1)))
1988-
.apply(move |s_row, r_row| Zip::from(s_row).and(r_row).apply(|a, b| f(a, b)));
1988+
.for_each(move |s_row, r_row| Zip::from(s_row).and(r_row).for_each(|a, b| f(a, b)));
19891989
}
19901990

19911991
fn zip_mut_with_elem<B, F>(&mut self, rhs_elem: &B, mut f: F)
@@ -2343,7 +2343,7 @@ where
23432343
prev.slice_axis_inplace(axis, Slice::from(..-1));
23442344
curr.slice_axis_inplace(axis, Slice::from(1..));
23452345
// This implementation relies on `Zip` iterating along `axis` in order.
2346-
Zip::from(prev).and(curr).apply(|prev, curr| unsafe {
2346+
Zip::from(prev).and(curr).for_each(|prev, curr| unsafe {
23472347
// These pointer dereferences and borrows are safe because:
23482348
//
23492349
// 1. They're pointers to elements in the array.

src/linalg/impl_linalg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,11 +679,11 @@ unsafe fn general_mat_vec_mul_impl<A, S1, S2>(
679679

680680
if beta.is_zero() {
681681
// when beta is zero, c may be uninitialized
682-
Zip::from(a.outer_iter()).and(y).apply(|row, elt| {
682+
Zip::from(a.outer_iter()).and(y).for_each(|row, elt| {
683683
elt.write(row.dot(x) * alpha);
684684
});
685685
} else {
686-
Zip::from(a.outer_iter()).and(y).apply(|row, elt| {
686+
Zip::from(a.outer_iter()).and(y).for_each(|row, elt| {
687687
*elt = *elt * beta + row.dot(x) * alpha;
688688
});
689689
}

src/parallel/impl_par_methods.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,25 @@ macro_rules! zip_impl {
5959
D: Dimension,
6060
$($p: NdProducer<Dim=D> ,)*
6161
{
62+
/// The `par_for_each` method for `Zip`.
63+
///
64+
/// This is a shorthand for using `.into_par_iter().for_each()` on
65+
/// `Zip`.
66+
///
67+
/// Requires crate feature `rayon`.
68+
pub fn par_for_each<F>(self, function: F)
69+
where F: Fn($($p::Item),*) + Sync + Send
70+
{
71+
self.into_par_iter().for_each(move |($($p,)*)| function($($p),*))
72+
}
73+
6274
/// The `par_apply` method for `Zip`.
6375
///
6476
/// This is a shorthand for using `.into_par_iter().for_each()` on
6577
/// `Zip`.
6678
///
6779
/// Requires crate feature `rayon`.
80+
#[deprecated(note="Renamed to .par_for_each()", since="0.15.0")]
6881
pub fn par_apply<F>(self, function: F)
6982
where F: Fn($($p::Item),*) + Sync + Send
7083
{
@@ -73,11 +86,11 @@ macro_rules! zip_impl {
7386

7487
expand_if!(@bool [$notlast]
7588

76-
/// Apply and collect the results into a new array, which has the same size as the
89+
/// Map and collect the results into a new array, which has the same size as the
7790
/// inputs.
7891
///
7992
/// If all inputs are c- or f-order respectively, that is preserved in the output.
80-
pub fn par_apply_collect<R>(self, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
93+
pub fn par_map_collect<R>(self, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
8194
-> Array<R, D>
8295
where R: Send
8396
{
@@ -122,21 +135,48 @@ macro_rules! zip_impl {
122135
}
123136
}
124137

125-
/// Apply and assign the results into the producer `into`, which should have the same
138+
/// Map and collect the results into a new array, which has the same size as the
139+
/// inputs.
140+
///
141+
/// If all inputs are c- or f-order respectively, that is preserved in the output.
142+
#[deprecated(note="Renamed to .par_map_collect()", since="0.15.0")]
143+
pub fn par_apply_collect<R>(self, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
144+
-> Array<R, D>
145+
where R: Send
146+
{
147+
self.par_map_collect(f)
148+
}
149+
150+
/// Map and assign the results into the producer `into`, which should have the same
126151
/// size as the other inputs.
127152
///
128153
/// The producer should have assignable items as dictated by the `AssignElem` trait,
129154
/// for example `&mut R`.
130-
pub fn par_apply_assign_into<R, Q>(self, into: Q, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
155+
pub fn par_map_assign_into<R, Q>(self, into: Q, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
131156
where Q: IntoNdProducer<Dim=D>,
132157
Q::Item: AssignElem<R> + Send,
133158
Q::Output: Send,
134159
{
135160
self.and(into)
136-
.par_apply(move |$($p, )* output_| {
161+
.par_for_each(move |$($p, )* output_| {
137162
output_.assign_elem(f($($p ),*));
138163
});
139164
}
165+
166+
/// Apply and assign the results into the producer `into`, which should have the same
167+
/// size as the other inputs.
168+
///
169+
/// The producer should have assignable items as dictated by the `AssignElem` trait,
170+
/// for example `&mut R`.
171+
#[deprecated(note="Renamed to .par_map_assign_into()", since="0.15.0")]
172+
pub fn par_apply_assign_into<R, Q>(self, into: Q, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
173+
where Q: IntoNdProducer<Dim=D>,
174+
Q::Item: AssignElem<R> + Send,
175+
Q::Output: Send,
176+
{
177+
self.par_map_assign_into(into, f)
178+
}
179+
140180
);
141181
}
142182
)+

src/parallel/zipmacro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/// Is equivalent to:
2525
///
2626
/// ```rust,ignore
27-
/// Zip::from(&mut a).and(&b).and(&c).par_apply(|a, &b, &c| {
27+
/// Zip::from(&mut a).and(&b).and(&c).par_for_each(|a, &b, &c| {
2828
/// *a = b + c;
2929
/// });
3030
/// ```
@@ -54,6 +54,6 @@
5454
/// ```
5555
macro_rules! par_azip {
5656
($($t:tt)*) => {
57-
$crate::azip!(@build par_apply $($t)*)
57+
$crate::azip!(@build par_for_each $($t)*)
5858
};
5959
}

src/traversal_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ pub(crate) fn assign_to<'a, P1, P2, A>(from: P1, to: P2)
2121
A: Clone + 'a
2222
{
2323
Zip::from(from)
24-
.apply_assign_into(to, A::clone);
24+
.map_assign_into(to, A::clone);
2525
}
2626

src/zip/mod.rs

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl<A, D: Dimension> NdProducer for RawArrayViewMut<A, D> {
518518
/// The order elements are visited is not specified. The producers don’t have to
519519
/// have the same item type.
520520
///
521-
/// The `Zip` has two methods for function application: `apply` and
521+
/// The `Zip` has two methods for function application: `for_each` and
522522
/// `fold_while`. The zip object can be split, which allows parallelization.
523523
/// A read-only zip object (no mutable producers) can be cloned.
524524
///
@@ -546,7 +546,7 @@ impl<A, D: Dimension> NdProducer for RawArrayViewMut<A, D> {
546546
/// .and(&b)
547547
/// .and(&c)
548548
/// .and(&d)
549-
/// .apply(|w, &x, &y, &z| {
549+
/// .for_each(|w, &x, &y, &z| {
550550
/// *w += x + y * z;
551551
/// });
552552
///
@@ -563,7 +563,7 @@ impl<A, D: Dimension> NdProducer for RawArrayViewMut<A, D> {
563563
///
564564
/// Zip::from(&mut totals)
565565
/// .and(a.rows())
566-
/// .apply(|totals, row| *totals = row.sum());
566+
/// .for_each(|totals, row| *totals = row.sum());
567567
///
568568
/// // Check the result against the built in `.sum_axis()` along axis 1.
569569
/// assert_eq!(totals, a.sum_axis(Axis(1)));
@@ -692,21 +692,21 @@ impl<P, D> Zip<P, D>
692692
where
693693
D: Dimension,
694694
{
695-
fn apply_core<F, Acc>(&mut self, acc: Acc, mut function: F) -> FoldWhile<Acc>
695+
fn for_each_core<F, Acc>(&mut self, acc: Acc, mut function: F) -> FoldWhile<Acc>
696696
where
697697
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
698698
P: ZippableTuple<Dim = D>,
699699
{
700700
if self.dimension.ndim() == 0 {
701701
function(acc, unsafe { self.parts.as_ref(self.parts.as_ptr()) })
702702
} else if self.layout.is(CORDER | FORDER) {
703-
self.apply_core_contiguous(acc, function)
703+
self.for_each_core_contiguous(acc, function)
704704
} else {
705-
self.apply_core_strided(acc, function)
705+
self.for_each_core_strided(acc, function)
706706
}
707707
}
708708

709-
fn apply_core_contiguous<F, Acc>(&mut self, acc: Acc, mut function: F) -> FoldWhile<Acc>
709+
fn for_each_core_contiguous<F, Acc>(&mut self, acc: Acc, mut function: F) -> FoldWhile<Acc>
710710
where
711711
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
712712
P: ZippableTuple<Dim = D>,
@@ -744,7 +744,7 @@ where
744744
}
745745

746746

747-
fn apply_core_strided<F, Acc>(&mut self, acc: Acc, function: F) -> FoldWhile<Acc>
747+
fn for_each_core_strided<F, Acc>(&mut self, acc: Acc, function: F) -> FoldWhile<Acc>
748748
where
749749
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
750750
P: ZippableTuple<Dim = D>,
@@ -754,14 +754,14 @@ where
754754
panic!("Unreachable: ndim == 0 is contiguous")
755755
}
756756
if n == 1 || self.layout_tendency >= 0 {
757-
self.apply_core_strided_c(acc, function)
757+
self.for_each_core_strided_c(acc, function)
758758
} else {
759-
self.apply_core_strided_f(acc, function)
759+
self.for_each_core_strided_f(acc, function)
760760
}
761761
}
762762

763763
// Non-contiguous but preference for C - unroll over Axis(ndim - 1)
764-
fn apply_core_strided_c<F, Acc>(&mut self, mut acc: Acc, mut function: F) -> FoldWhile<Acc>
764+
fn for_each_core_strided_c<F, Acc>(&mut self, mut acc: Acc, mut function: F) -> FoldWhile<Acc>
765765
where
766766
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
767767
P: ZippableTuple<Dim = D>,
@@ -785,7 +785,7 @@ where
785785
}
786786

787787
// Non-contiguous but preference for F - unroll over Axis(0)
788-
fn apply_core_strided_f<F, Acc>(&mut self, mut acc: Acc, mut function: F) -> FoldWhile<Acc>
788+
fn for_each_core_strided_f<F, Acc>(&mut self, mut acc: Acc, mut function: F) -> FoldWhile<Acc>
789789
where
790790
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
791791
P: ZippableTuple<Dim = D>,
@@ -939,15 +939,24 @@ macro_rules! map_impl {
939939
{
940940
/// Apply a function to all elements of the input arrays,
941941
/// visiting elements in lock step.
942-
pub fn apply<F>(mut self, mut function: F)
942+
pub fn for_each<F>(mut self, mut function: F)
943943
where F: FnMut($($p::Item),*)
944944
{
945-
self.apply_core((), move |(), args| {
945+
self.for_each_core((), move |(), args| {
946946
let ($($p,)*) = args;
947947
FoldWhile::Continue(function($($p),*))
948948
});
949949
}
950950

951+
/// Apply a function to all elements of the input arrays,
952+
/// visiting elements in lock step.
953+
#[deprecated(note="Renamed to .for_each()", since="0.15.0")]
954+
pub fn apply<F>(self, function: F)
955+
where F: FnMut($($p::Item),*)
956+
{
957+
self.for_each(function)
958+
}
959+
951960
/// Apply a fold function to all elements of the input arrays,
952961
/// visiting elements in lock step.
953962
///
@@ -980,7 +989,7 @@ macro_rules! map_impl {
980989
where
981990
F: FnMut(Acc, $($p::Item),*) -> Acc,
982991
{
983-
self.apply_core(acc, move |acc, args| {
992+
self.for_each_core(acc, move |acc, args| {
984993
let ($($p,)*) = args;
985994
FoldWhile::Continue(function(acc, $($p),*))
986995
}).into_inner()
@@ -993,7 +1002,7 @@ macro_rules! map_impl {
9931002
-> FoldWhile<Acc>
9941003
where F: FnMut(Acc, $($p::Item),*) -> FoldWhile<Acc>
9951004
{
996-
self.apply_core(acc, move |acc, args| {
1005+
self.for_each_core(acc, move |acc, args| {
9971006
let ($($p,)*) = args;
9981007
function(acc, $($p),*)
9991008
})
@@ -1015,7 +1024,7 @@ macro_rules! map_impl {
10151024
pub fn all<F>(mut self, mut predicate: F) -> bool
10161025
where F: FnMut($($p::Item),*) -> bool
10171026
{
1018-
!self.apply_core((), move |_, args| {
1027+
!self.for_each_core((), move |_, args| {
10191028
let ($($p,)*) = args;
10201029
if predicate($($p),*) {
10211030
FoldWhile::Continue(())
@@ -1065,12 +1074,11 @@ macro_rules! map_impl {
10651074
}
10661075
}
10671076

1068-
/// Apply and collect the results into a new array, which has the same size as the
1077+
/// Map and collect the results into a new array, which has the same size as the
10691078
/// inputs.
10701079
///
10711080
/// If all inputs are c- or f-order respectively, that is preserved in the output.
1072-
pub fn apply_collect<R>(self, f: impl FnMut($($p::Item,)* ) -> R) -> Array<R, D>
1073-
{
1081+
pub fn map_collect<R>(self, f: impl FnMut($($p::Item,)* ) -> R) -> Array<R, D> {
10741082
// Make uninit result
10751083
let mut output = self.uninitalized_for_current_layout::<R>();
10761084

@@ -1086,21 +1094,43 @@ macro_rules! map_impl {
10861094
}
10871095
}
10881096

1089-
/// Apply and assign the results into the producer `into`, which should have the same
1097+
/// Map and collect the results into a new array, which has the same size as the
1098+
/// inputs.
1099+
///
1100+
/// If all inputs are c- or f-order respectively, that is preserved in the output.
1101+
#[deprecated(note="Renamed to .map_collect()", since="0.15.0")]
1102+
pub fn apply_collect<R>(self, f: impl FnMut($($p::Item,)* ) -> R) -> Array<R, D> {
1103+
self.map_collect(f)
1104+
}
1105+
1106+
/// Map and assign the results into the producer `into`, which should have the same
10901107
/// size as the other inputs.
10911108
///
10921109
/// The producer should have assignable items as dictated by the `AssignElem` trait,
10931110
/// for example `&mut R`.
1094-
pub fn apply_assign_into<R, Q>(self, into: Q, mut f: impl FnMut($($p::Item,)* ) -> R)
1111+
pub fn map_assign_into<R, Q>(self, into: Q, mut f: impl FnMut($($p::Item,)* ) -> R)
10951112
where Q: IntoNdProducer<Dim=D>,
10961113
Q::Item: AssignElem<R>
10971114
{
10981115
self.and(into)
1099-
.apply(move |$($p, )* output_| {
1116+
.for_each(move |$($p, )* output_| {
11001117
output_.assign_elem(f($($p ),*));
11011118
});
11021119
}
11031120

1121+
/// Map and assign the results into the producer `into`, which should have the same
1122+
/// size as the other inputs.
1123+
///
1124+
/// The producer should have assignable items as dictated by the `AssignElem` trait,
1125+
/// for example `&mut R`.
1126+
#[deprecated(note="Renamed to .map_assign_into()", since="0.15.0")]
1127+
pub fn apply_assign_into<R, Q>(self, into: Q, f: impl FnMut($($p::Item,)* ) -> R)
1128+
where Q: IntoNdProducer<Dim=D>,
1129+
Q::Item: AssignElem<R>
1130+
{
1131+
self.map_assign_into(into, f)
1132+
}
1133+
11041134

11051135
);
11061136

@@ -1150,7 +1180,7 @@ macro_rules! map_impl {
11501180
// Apply the mapping function on this zip
11511181
// if we panic with unwinding; Partial will drop the written elements.
11521182
let partial_len = &mut partial.len;
1153-
self.apply(move |$($p,)* output_elem: *mut R| {
1183+
self.for_each(move |$($p,)* output_elem: *mut R| {
11541184
output_elem.write(f($($p),*));
11551185
if std::mem::needs_drop::<R>() {
11561186
*partial_len += 1;

0 commit comments

Comments
 (0)