use veho::matrix::{init, Matrix}; use veho::vector::Mappers; pub fn transpose(matrix: M) -> Matrix where M: IntoIterator, R: IntoIterator, R::IntoIter: ExactSizeIterator, { let mut iters = (&mut matrix.into_iter()) .map(|row| row.into_iter()) .collect::>(); let width = iters.first().map_or(0, |iter| iter.len()); (0..width).map(|_| { (&mut iters).mapper(|it| it.next().unwrap()) }).collect::>() } pub fn columns_mapper(matrix: M, mut f: F) -> Matrix where M: IntoIterator, R: IntoIterator, R::IntoIter: ExactSizeIterator, F: FnMut(R::Item) -> T, { let mut iters = (&mut matrix.into_iter()) .map(|row| row.into_iter()) .collect::>(); let width = iters.first().map_or(0, |iter| iter.len()); (0..width).map(|_| (&mut iters).mapper(|it| f(it.next().unwrap())) ).collect::>() } #[test] fn test_columns_mapper() { let matrix = init(4, 4, |i, j| (i + 1) * 10 + j * 2); println!("columns_mapped {:?}", columns_mapper(&matrix, |x| x * 2)); println!("transposed {:?}", transpose(&matrix)); println!("original {:?}", matrix) // println!("{:?}", column(matrix, 2)); }