Skip to content

Commit

Permalink
feat: add a map and map_ref method
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Dec 16, 2024
1 parent 6780239 commit c1e3c01
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,60 @@ impl<T> Grid<T> {
self.data.fill_with(f);
}

/// Returns a new grid with the same dimensions, but with each element transformed by the closure.
///
/// # Examples
///
/// ```
/// use grid::*;
/// let grid = grid![[1,2][3,4]];
/// let new_grid = grid.map(|x| x * 2);
/// assert_eq!(new_grid, grid![[2,4][6,8]]);
///
/// let grid = grid![[1,2][3,4]];
/// let new_grid = grid.map(|x| x > 2);
/// assert_eq!(new_grid, grid![[false,false][true,true]]);
/// ```
pub fn map<U, F>(self, f: F) -> Grid<U>
where
F: FnMut(T) -> U,
{
Grid {
data: self.data.into_iter().map(f).collect(),
cols: self.cols,
rows: self.rows,
order: self.order,
}
}

/// Returns a new grid with the same dimensions, but with each element
/// transformed by the closure. Does not consume the grid.
///
/// # Examples
///
/// ```
/// use grid::*;
/// let grid = grid![[1,2][3,4]];
/// let new_grid = grid.map(|x| x * 2);
/// assert_eq!(new_grid, grid![[2,4][6,8]]);
///
/// let grid = grid![[1,2][3,4]];
/// let new_grid = grid.map(|x| x > 2);
/// assert_eq!(new_grid, grid![[false,false][true,true]]);
/// ```
#[must_use]
pub fn map_ref<U, F>(&self, f: F) -> Grid<U>
where
F: Fn(&T) -> U,
{
Grid {
data: self.data.iter().map(f).collect(),
cols: self.cols,
rows: self.rows,
order: self.order,
}
}

/// Iterate over the rows of the grid. Each time an iterator over a single
/// row is returned.
///
Expand Down

0 comments on commit c1e3c01

Please sign in to comment.