grid

The grid module provides a two-dimensional data structure. It is implemented as a dictionary with the keys being the a tuple of i,j indices of the elements.

Types

A single element value together with its position.

pub type Element(a) =
  #(Position, a)

A grid is a dictionary with the keys being the a tuple of y,x coordinates of the elements.

pub type Grid(a) =
  dict.Dict(Position, a)

A position is given as a tuple of coordinates: (y, x)

pub type Position =
  #(Int, Int)

Functions

pub fn from_lists(lists: List(List(a))) -> Dict(#(Int, Int), a)

Takes a m⨯n list of lists and returns a Grid - which is a dictionary with the keys being the a tuple of i,j indices of the elements.

pub fn get(
  grid: Dict(#(Int, Int), a),
  indices: #(Int, Int),
) -> Result(a, Nil)

Get value specified by indices

pub fn get_column(
  grid: Dict(#(Int, Int), a),
  index: Int,
) -> Result(List(a), Nil)

Gets the column of a given index as a list

pub fn get_indexed_column(
  grid: Dict(#(Int, Int), a),
  index: Int,
) -> Result(List(#(#(Int, Int), a)), Nil)

Gets the column of a given index as a list of tuples of indices value

pub fn get_indexed_row(
  grid: Dict(#(Int, Int), a),
  index: Int,
) -> Result(List(#(#(Int, Int), a)), Nil)

Gets the row of a given index as a list of tuples of indices and values

pub fn get_row(
  grid: Dict(#(Int, Int), a),
  index: Int,
) -> Result(List(a), Nil)

Gets the row of a given index as a list

pub fn position_to_string(position: #(Int, Int)) -> String

Transforms a given position to string (for debug purposes)

pub fn pretty_print(
  grid: Dict(#(Int, Int), String),
) -> Dict(#(Int, Int), String)

Given a Grid of Strings, pretty prints it

pub fn size(grid: Dict(#(Int, Int), a)) -> #(Int, Int)

Gets the height and width of a given Grid as a tuple #(height, width)

pub fn to_col_list_indexed(
  grid: Dict(#(Int, Int), a),
) -> List(List(#(#(Int, Int), a)))

Transforms a grid to a list of list of tuples of indices and values (column-wise)

pub fn to_lists(grid: Dict(#(Int, Int), a)) -> List(List(a))

Transforms a grid to a list of lists (row-wise)

pub fn to_row_list_indexed(
  grid: Dict(#(Int, Int), a),
) -> List(List(#(#(Int, Int), a)))

Transforms a grid to a list of lists of tuples of indices and values (row-wise)

pub fn update_if_exists(
  grid: Dict(#(Int, Int), a),
  indices: #(Int, Int),
  value: a,
) -> Dict(#(Int, Int), a)

Updates a given entry (specified by a tuple of indices) only if it already exists.

Search Document