# Types

## Space

</>

An n-dimensional `Space` containing `Axes` of various sizes.

``pub opaque type Space``

## SpaceError

</>

An error returned when attempting to create an invalid `Space`.

``````pub type SpaceError {
CannotMerge
CannotInfer
DuplicateName
InvalidSize
SpaceError(reason: SpaceError, axes: Axes)
}``````

### Constructors

• ``CannotMerge``
• ``CannotInfer``
• ``DuplicateName``
• ``InvalidSize``
• ``SpaceError(reason: SpaceError, axes: Axes)``

## SpaceErrors

</>

A `SpaceError` list.

``````pub type SpaceErrors =
List(SpaceError)``````

## SpaceResult

</>

A `Result` alias type for spaces.

``````pub type SpaceResult =
Result(Space, SpaceErrors)``````

# Functions

## axes

</>
``pub fn axes(x: Space) -> List(Axis)``

Returns the axes of a given `Space`.

## Examples

``````> new() |> axes
[]

> import argamak/axis.{A, B, Infer}
> let assert Ok(space) = d1(Infer("A"))
> axes(space)
[Infer("A")]

> let assert Ok(space) = d3(A(2), B(2), Infer("C"))
> axes(space)
[A(2), B(2), Infer("C")]
``````

## d1

</>
``pub fn d1(a: Axis) -> Result(Space, List(SpaceError))``

Results in a one-dimensional `Space` on success, or `SpaceErrors` on failure.

## Examples

``````> import argamak/axis.{Infer}
> let assert Ok(space) = d1(Infer("A"))
> axes(space)
[Infer("A")]
``````

## d2

</>
``pub fn d2(a: Axis, b: Axis) -> Result(Space, List(SpaceError))``

Results in a two-dimensional `Space` on success, or `SpaceErrors` on failure.

## Examples

``````> import argamak/axis.{A, B}
> let assert Ok(space) = d2(A(2), B(2))
> axes(space)
[A(2), B(2)]
``````

## d3

</>
``````pub fn d3(a: Axis, b: Axis, c: Axis) -> Result(
Space,
List(SpaceError),
)``````

Results in a three-dimensional `Space` on success, or `SpaceErrors` on failure.

## Examples

``````> import argamak/axis.{A, B, Infer}
> let assert Ok(space) = d3(A(2), B(2), Infer("C"))
> axes(space)
[A(2), B(2), Infer("C")]
``````

## d4

</>
``````pub fn d4(a: Axis, b: Axis, c: Axis, d: Axis) -> Result(
Space,
List(SpaceError),
)``````

Results in a four-dimensional `Space` on success, or `SpaceErrors` on failure.

## Examples

``````> import argamak/axis.{A, B, D, Infer}
> let assert Ok(space) = d4(A(2), B(2), Infer("C"), D(1))
> axes(space)
[A(2), B(2), Infer("C"), D(1)]
``````

## d5

</>
``````pub fn d5(a: Axis, b: Axis, c: Axis, d: Axis, e: Axis) -> Result(
Space,
List(SpaceError),
)``````

Results in a five-dimensional `Space` on success, or `SpaceErrors` on failure.

## Examples

``````> import argamak/axis.{A, B, C, D, E}
> let assert Ok(space) = d5(A(5), B(4), C(3), D(2), E(1))
> axes(space)
[A(5), B(4), C(3), D(2), E(1)]
``````

## d6

</>
``````pub fn d6(a: Axis, b: Axis, c: Axis, d: Axis, e: Axis, f: Axis) -> Result(
Space,
List(SpaceError),
)``````

Results in a six-dimensional `Space` on success, or `SpaceErrors` on failure.

## Examples

``````> import argamak/axis.{A, B, C, D, E, F}
> let assert Ok(space) = d6(A(9), B(9), C(9), D(9), E(9), F(9))
> axes(space)
[A(9), B(9), C(9), D(9), E(9), F(9)]
``````

## degree

</>
``pub fn degree(x: Space) -> Int``

Returns the degree of a given `Space`.

## Examples

``````> new() |> degree
0

> import argamak/axis.{A, B, Infer}
> let assert Ok(space) = d1(Infer("A"))
> degree(space)
1

> let assert Ok(space) = d3(A(2), B(2), Infer("C"))
> degree(space)
3
``````

## from_list

</>
``pub fn from_list(x: List(Axis)) -> Result(Space, List(SpaceError))``

Results in a `Space` created from a list of `Axes` on success, or `SpaceErrors` on failure.

## Examples

``````> import argamak/axis.{A, B, C, D, E, F, Z}
> let assert Ok(space) = from_list([A(9), B(9), C(9), D(9), E(9), F(9), Z(9)])
> axes(space)
[A(9), B(9), C(9), D(9), E(9), F(9), Z(9)]
``````

## map

</>
``````pub fn map(x: Space, with fun: fn(Axis) -> Axis) -> Result(
Space,
List(SpaceError),
)``````

Results in a new `Space` with the same number of dimensions as the given `Space` on success, or `SpaceErrors` on failure.

Applies the given function to each `Axis` of the `Space`.

## Examples

``````> import argamak/axis.{B, C, Infer}
> let assert Ok(space) = map(new(), with: fn(_) { C(3) })
> axes(space)
[]

> let assert Ok(space) = d1(Infer("A"))
> let assert Ok(space) = map(space, with: fn(_) { C(3) })
> axes(space)
[C(3)]

> let assert Ok(space) = d3(Infer("A"), B(2), C(2))
> let assert Ok(space) = map(space, with: fn(axis) {
>   case axis {
>     Infer(_) -> axis.resize(axis, 4)
>     _else -> axis
>   }
> })
> axes(space)
[A(4), B(2), C(2)]
``````

## merge

</>
``````pub fn merge(a: Space, b: Space) -> Result(
Space,
List(SpaceError),
)``````

Results in a new `Space` that is the element-wise maximum of the given spaces on success, or `SpaceErrors` on failure.

Spaces are merged tail-first, and corresponding `Axis` names must match.

## Examples

``````> import argamak/axis.{Axis, Infer, X, Y}
> let assert Ok(a) = d1(Infer("X"))
> merge(a, new()) |> result.map(with: axes)
Ok([Infer("X")])

> let assert Ok(b) = d2(Axis("Sparkle", 2), X(2))
> merge(a, b) |> result.map(with: axes)
Ok([Axis("Sparkle", 2), Infer("X")])

> let assert Ok(c) = d3(Infer("X"), Axis("Sparkle", 3), Y(3))
> merge(b, c)
Error([SpaceError(CannotMerge, [Y(3), X(2)])])
``````

## new

</>
``pub fn new() -> Space``

Results in a dimensionless `Space`.

## Examples

``````> new() |> axes
[]
``````

## shape

</>
``pub fn shape(x: Space) -> List(Int)``

Returns the shape of a given `Space`.

## Examples

``````> new() |> shape
[]

> import argamak/axis.{A, B, Infer}
> let assert Ok(space) = d1(Infer("A"))
> shape(space)
[0]

> let assert Ok(space) = d3(A(2), B(2), Infer("C"))
> shape(space)
[2, 2, 0]
``````

## to_string

</>
``pub fn to_string(x: Space) -> String``

Converts a `Space` into a `String`.

## Examples

``````> new() |> to_string
"Space()"

> import argamak/axis.{A, B, Axis, Infer}
> let assert Ok(space) = d1(Axis("Sparkle", 2))
> to_string(space)
"Space(Axis(\"Sparkle\", 2))"

> let assert Ok(space) = d3(A(2), B(2), Infer("C"))
> to_string(space)
"Space(A(2), B(2), Infer(\"C\"))"
``````