View Source Table (Table v0.1.0)

Unified access to tabular data.

Various data structures have a tabular representation, however to access this representation, manual conversion is required. On top of that, tabular access itself has two variants, a row-based one and a column-based one, each useful under different circumstances.

The Table package provides a thin layer that unifies access to tabular data in different formats.

protocol

Protocol

The unified access is enabled for structs implementing the Table.Reader protocol. Note that a struct may be representable as tabular data only in some cases, so the protocol implementation may be lax. Consequently, functions in this module will raise when given non-tabular data.

By default the protocol is implemented for lists and maps of certain shape.

# List of matching key-value lists
data = [
  [{"id", 1}, {"name", "Sherlock"}],
  [{"id", 2}, {"name", "John"}]
]

# List of matching maps
data = [
  %{"id" => 1, "name" => "Sherlock"},
  %{"id" => 2, "name" => "John"}
]

# List of column tuples
data = [
  {"id", 1..2},
  {"name", ["Sherlock", "John"]}
]

# Map with column values
data = %{
  "id" => [1, 2],
  "name" => ["Sherlock", "John"]
}

Link to this section Summary

Functions

Accesses tabular data as individual columns.

Same as to_columns/2, extended with information about the table.

Accesses tabular data as a sequence of rows.

Same as to_rows/2, extended with information about the table.

Link to this section Types

@type column() :: term()
@type table_info() :: %{columns: [column()]}

Link to this section Functions

Link to this function

to_columns(tabular, opts \\ [])

View Source
@spec to_columns(
  Table.Reader.t(),
  keyword()
) :: %{required(column()) => Enumerable.t()}

Accesses tabular data as individual columns.

Returns a map with enumerables as values.

options

Options

  • :only - specifies a subset of columns to include in the result

examples

Examples

iex> data = [%{id: 1, name: "Sherlock"}, %{id: 2, name: "John"}, %{id: 3, name: "Mycroft"}]
iex> columns = Table.to_columns(data)
iex> Enum.to_list(columns.id)
[1, 2, 3]
iex> Enum.to_list(columns.name)
["Sherlock", "John", "Mycroft"]
Link to this function

to_columns_with_info(tabular, opts \\ [])

View Source
@spec to_columns_with_info(
  Table.Reader.t(),
  keyword()
) :: {%{required(column()) => Enumerable.t()}, table_info()}

Same as to_columns/2, extended with information about the table.

examples

Examples

iex> data = [%{id: 1, name: "Sherlock"}, %{id: 2, name: "John"}, %{id: 3, name: "Mycroft"}]
iex> {_columns, info} = Table.to_columns_with_info(data)
iex> info
%{columns: [:id, :name]}
Link to this function

to_rows(tabular, opts \\ [])

View Source
@spec to_rows(
  Table.Reader.t(),
  keyword()
) :: Enumerable.t()

Accesses tabular data as a sequence of rows.

Returns an enumerable that emits each row as a map.

options

Options

  • :only - specifies a subset of columns to include in the result

examples

Examples

iex> data = %{id: [1, 2, 3], name: ["Sherlock", "John", "Mycroft"]}
iex> data |> Table.to_rows() |> Enum.to_list()
[%{id: 1, name: "Sherlock"}, %{id: 2, name: "John"}, %{id: 3, name: "Mycroft"}]

iex> data = [[id: 1, name: "Sherlock"], [id: 2, name: "John"], [id: 3, name: "Mycroft"]]
iex> data |> Table.to_rows() |> Enum.to_list()
[%{id: 1, name: "Sherlock"}, %{id: 2, name: "John"}, %{id: 3, name: "Mycroft"}]
Link to this function

to_rows_with_info(tabular, opts \\ [])

View Source
@spec to_rows_with_info(
  Table.Reader.t(),
  keyword()
) :: {Enumerable.t(), table_info()}

Same as to_rows/2, extended with information about the table.

examples

Examples

iex> data = %{id: [1, 2, 3], name: ["Sherlock", "John", "Mycroft"]}
iex> {_rows, info} = Table.to_rows_with_info(data)
iex> info
%{columns: [:id, :name]}