@@ -25,6 +25,7 @@ use crate::error::{self, ErrorKind, ShapeError};
2525use crate :: math_cell:: MathCell ;
2626use crate :: itertools:: zip;
2727use crate :: zip:: Zip ;
28+ use crate :: AxisDescription ;
2829
2930use crate :: iter:: {
3031 AxisChunksIter , AxisChunksIterMut , AxisIter , AxisIterMut , ExactChunks , ExactChunksMut ,
@@ -511,6 +512,63 @@ where
511512 debug_assert ! ( self . pointer_is_inbounds( ) ) ;
512513 }
513514
515+ /// Return a view of a slice of the array, with a closure specifying the
516+ /// slice for each axis.
517+ ///
518+ /// This is especially useful for code which is generic over the
519+ /// dimensionality of the array.
520+ ///
521+ /// **Panics** if an index is out of bounds or step size is zero.
522+ pub fn slice_each_axis < F > ( & self , f : F ) -> ArrayView < ' _ , A , D >
523+ where
524+ F : FnMut ( AxisDescription ) -> Slice ,
525+ S : Data ,
526+ {
527+ let mut view = self . view ( ) ;
528+ view. slice_each_axis_inplace ( f) ;
529+ view
530+ }
531+
532+ /// Return a mutable view of a slice of the array, with a closure
533+ /// specifying the slice for each axis.
534+ ///
535+ /// This is especially useful for code which is generic over the
536+ /// dimensionality of the array.
537+ ///
538+ /// **Panics** if an index is out of bounds or step size is zero.
539+ pub fn slice_each_axis_mut < F > ( & mut self , f : F ) -> ArrayViewMut < ' _ , A , D >
540+ where
541+ F : FnMut ( AxisDescription ) -> Slice ,
542+ S : DataMut ,
543+ {
544+ let mut view = self . view_mut ( ) ;
545+ view. slice_each_axis_inplace ( f) ;
546+ view
547+ }
548+
549+ /// Slice the array in place, with a closure specifying the slice for each
550+ /// axis.
551+ ///
552+ /// This is especially useful for code which is generic over the
553+ /// dimensionality of the array.
554+ ///
555+ /// **Panics** if an index is out of bounds or step size is zero.
556+ pub fn slice_each_axis_inplace < F > ( & mut self , mut f : F )
557+ where
558+ F : FnMut ( AxisDescription ) -> Slice ,
559+ {
560+ ( 0 ..self . ndim ( ) ) . for_each ( |ax| {
561+ self . slice_axis_inplace (
562+ Axis ( ax) ,
563+ f ( AxisDescription (
564+ Axis ( ax) ,
565+ self . dim [ ax] ,
566+ self . strides [ ax] as isize ,
567+ ) ) ,
568+ )
569+ } )
570+ }
571+
514572 /// Return a reference to the element at `index`, or return `None`
515573 /// if the index is out of bounds.
516574 ///
0 commit comments