argamak/space
Types
An n-dimensional Space
with axis–shape tuple elements, one per dimension.
pub opaque type Space(dn, axis)
An error returned when attempting to create an invalid Space
.
pub type SpaceError {
SpaceError(error: String, element: String)
}
Constructors
-
SpaceError(error: String, element: String)
A SpaceError
list.
pub type SpaceErrors =
List(SpaceError)
Functions
pub fn axes(of space: Space(a, b)) -> List(b)
Returns the axes of a given Space
.
Examples
> assert Ok(space) = d0()
> axes(space)
[]
> type Axis { A B C }
> assert Ok(space) = d1(#(A, -1))
> axes(space)
[A]
> assert Ok(space) = d3(#(A, 2), #(B, 2), #(C, -1))
> axes(space)
[A, B, C]
pub fn d0() -> Result(Space(D0, a), List(SpaceError))
Results in a dimensionless Space
.
Examples
> assert Ok(space) = d0()
> elements(space)
[]
pub fn d1(a: #(a, Int)) -> Result(Space(D1, a), List(SpaceError))
Results in a 1-dimensional Space
on success, or SpaceErrors
on failure.
Examples
> type Axis { A }
> assert Ok(space) = d1(#(A, -1))
> elements(space)
[#(A, -1)]
pub fn d2(a: #(a, Int), b: #(a, Int)) -> Result(
Space(D2, a),
List(SpaceError),
)
Results in a 2-dimensional Space
on success, or SpaceErrors
on failure.
Examples
> type Axis { A B }
> assert Ok(space) = d2(#(A, 2), #(B, 2))
> elements(space)
[#(A, 2), #(B, 2)]
pub fn d3(a: #(a, Int), b: #(a, Int), c: #(a, Int)) -> Result(
Space(D3, a),
List(SpaceError),
)
Results in a 3-dimensional Space
on success, or SpaceErrors
on failure.
Examples
> type Axis { A B C }
> assert Ok(space) = d3(#(A, 2), #(B, 2), #(C, -1))
> elements(space)
[#(A, 2), #(B, 2), #(C, -1)]
pub fn d4(a: #(a, Int), b: #(a, Int), c: #(a, Int), d: #(a, Int)) -> Result(
Space(D4, a),
List(SpaceError),
)
Results in a 4-dimensional Space
on success, or SpaceErrors
on failure.
Examples
> type Axis { A B C D }
> assert Ok(space) = d4(#(A, 2), #(B, 2), #(C, -1), #(D, 1))
> elements(space)
[#(A, 2), #(B, 2), #(C, -1), #(D, 1)]
pub fn d5(a: #(a, Int), b: #(a, Int), c: #(a, Int), d: #(a, Int), e: #(
a,
Int,
)) -> Result(Space(D5, a), List(SpaceError))
Results in a 5-dimensional Space
on success, or SpaceErrors
on failure.
Examples
> type Axis { A B C D E }
> assert Ok(space) = d5(#(A, 5), #(B, 4), #(C, 3), #(D, 2), #(E, 1))
> elements(space)
[#(A, 5), #(B, 4), #(C, 3), #(D, 2), #(E, 1)]
pub fn d6(a: #(a, Int), b: #(a, Int), c: #(a, Int), d: #(a, Int), e: #(
a,
Int,
), f: #(a, Int)) -> Result(Space(D6, a), List(SpaceError))
Results in a 6-dimensional Space
on success, or SpaceErrors
on failure.
Examples
> type Axis { A B C D E F }
> assert Ok(space) =
> d6(#(A, 9), #(B, 9), #(C, 9), #(D, 9), #(E, 9), #(F, 9))
> elements(space)
[#(A, 9), #(B, 9), #(C, 9), #(D, 9), #(E, 9), #(F, 9)]
pub fn degree(of space: Space(a, b)) -> a
Returns the degree of a given Space
.
Examples
> assert Ok(space) = d0()
> degree(space)
D0
> type Axis { A B C }
> assert Ok(space) = d1(#(A, -1))
> degree(space)
D1
> assert Ok(space) = d3(#(A, 2), #(B, 2), #(C, -1))
> degree(space)
D3
pub fn elements(of space: Space(a, b)) -> List(#(b, Int))
Returns the elements of a given Space
.
Examples
> assert Ok(space) = d0()
> elements(space)
[]
> type Axis { A B C }
> assert Ok(space) = d1(#(A, -1))
> elements(space)
[#(A, -1)]
> assert Ok(space) = d3(#(A, 2), #(B, 2), #(C, -1))
> elements(space)
[#(A, 2), #(B, 2), #(C, -1)]
pub fn map_elements(of space: Space(a, b), with fun: fn(#(b, Int)) ->
#(c, Int)) -> Result(Space(a, c), 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 element of the Space
.
Examples
> type Axis { A B C }
> assert Ok(space) = d0()
> assert Ok(space) = map_elements(of: space, with: fn(_) { #(C, 3) })
> elements(space)
[]
> assert Ok(space) = d1(#(A, -1))
> assert Ok(space) = map_elements(of: space, with: fn(_) { #(C, 3) })
> elements(space)
[#(C, 3)]
> assert Ok(space) = d3(#(A, -1), #(B, 2), #(C, 2))
> assert Ok(space) = map_elements(of: space, with: fn(element) {
> let #(axis, size) = element
> case size == -1 {
> True -> #(axis, 4)
> False -> element
> }
> })
> elements(space)
[#(A, 4), #(B, 2), #(C, 2)]
pub fn shape(of space: Space(a, b)) -> List(Int)
Returns the shape of a given Space
.
Examples
> assert Ok(space) = d0()
> shape(space)
[]
> type Axis { A B C }
> assert Ok(space) = d1(#(A, -1))
> shape(space)
[-1]
> assert Ok(space) = d3(#(A, 2), #(B, 2), #(C, -1))
> shape(space)
[2, 2, -1]
pub fn to_string(space: Space(a, b)) -> String
Converts a Space
into a String
.
Examples
> assert Ok(space) = d0()
> to_string(space)
"D0"
> type Axis { A B C }
> assert Ok(space) = d1(#(A, -1))
> to_string(space)
"D1 #(A, -1)"
> assert Ok(space) = d3(#(A, 2), #(B, 2), #(C, -1))
> to_string(space)
"D3 #(A, 2), #(B, 2), #(C, -1)"