Skip to content

Add indexed_map and indexed_map_ref methods to Grid#72

Draft
Copilot wants to merge 4 commits intomasterfrom
copilot/add-indexed-map-function
Draft

Add indexed_map and indexed_map_ref methods to Grid#72
Copilot wants to merge 4 commits intomasterfrom
copilot/add-indexed-map-function

Conversation

Copy link
Contributor

Copilot AI commented Feb 16, 2026

Adds indexed_map methods that transform grid elements with access to their (row, col) coordinates.

Changes

  • indexed_map<U, F>(self, f: F) -> Grid<U> - Consuming variant that maps over (index, value) pairs via indexed_into_iter()
  • indexed_map_ref<U, F>(&self, f: F) -> Grid<U> - Borrowing variant via indexed_iter()
  • Both preserve grid dimensions and storage order (RowMajor/ColumnMajor)
  • Marked with #[must_use] following the map/map_ref pattern

Usage

let grid: Grid<i32> = grid![[1,2][3,4]];

// Filter based on position
let lower_triangle: Grid<Option<i32>> = 
    grid.indexed_map(|(row, col), v| (row > col).then_some(v));
// Result: [[None, None], [Some(3), None]]

// Add coordinates to values
let result = grid.indexed_map_ref(|(row, col), v| v + row as i32 + col as i32);
// Result: [[1, 3], [4, 6]]
Original prompt

This section details on the original issue you should resolve

<issue_title>[feature request] Indexed map</issue_title>
<issue_description>Sometimes there is a need to use indexes inside the mapping function. For now there seem to be no convenient way to do it.

impl<T> Grid<T>
{
  fn indexed_map<U, F>(self, f: F) -> Grid<U>
  where F: Fn((usize, usize), T) -> U
  { todo!() }
}

and usage example

let grid: Grid<i32> = ...;
let grid2: Grid<Option<i32>> = grid.indexed_map(|(x, y), v| (x > y).then(v));

</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits February 16, 2026 18:59
Co-authored-by: becheran <22472855+becheran@users.noreply.github.com>
Co-authored-by: becheran <22472855+becheran@users.noreply.github.com>
Co-authored-by: becheran <22472855+becheran@users.noreply.github.com>
Copilot AI changed the title [WIP] Add indexed map function for Grid implementation Add indexed_map and indexed_map_ref methods to Grid Feb 16, 2026
Copilot AI requested a review from becheran February 16, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature request] Indexed map

2 participants