vec/vec4

Types

A 4-element structure that can be used to represent 4D coordinates or any other quadruplet of any values.

pub type Vec4(value) {
  Vec4(x: value, y: value, z: value, w: value)
}

Constructors

  • Vec4(x: value, y: value, z: value, w: value)

Values

pub fn from_tuple(
  tuple: #(value, value, value, value),
) -> Vec4(value)

Converts a tuple of the contained elements into a vector.

Examples

assert #(12, -34, 420, 69) |> from_tuple == Vec4(12, -34, 420, 69)
pub fn map(vector: Vec4(a), with fun: fn(a) -> b) -> Vec4(b)

Returns a new vector containing only the elements of the first vector after the function has been applied to each one.

Examples

assert
  Vec4(12, -34, 420, 69)
  |> map(fn(x) { x * 2 })
  == Vec4(24, -68, 840, 138)
pub fn map2(
  a: Vec4(a),
  b: Vec4(b),
  with fun: fn(a, b) -> c,
) -> Vec4(c)

Combines two vectors into a single vector using the given function.

Examples

assert
  map2(Vec4(12, -34, 420, 69), Vec4(1, 2, 3, 4), fn(x, y) { x * y })
  == Vec4(12, -68, 1260, 276)
pub fn map3(
  a: Vec4(a),
  b: Vec4(b),
  c: Vec4(c),
  with fun: fn(a, b, c) -> c,
) -> Vec4(c)

Combines three vectors into a single vector using the given function.

Examples

fn linear_interpolation(weight: Float, a: Float, b: Float) -> Float {
  a +. { b -. a } *. weight
}

assert
  splat(0.6)
  |> map3(
    Vec4(1.2, -3.4, 42.0, 0.69),
    Vec4(1.0, 2.1, -5.4, 7.5),
    linear_interpolation,
  )
  == Vec4(1.08, -0.1, 13.56, 4.78)
pub fn map_w(
  vector: Vec4(value),
  with fun: fn(value) -> value,
) -> Vec4(value)

Returns a new vector with the w element having had with applied to it.

Examples

assert
  Vec4(12, -34, 420, 69)
  |> map_w(fn(n) { n * 2 })
  == Vec4(12, -34, 420, 138)
pub fn map_x(
  vector: Vec4(value),
  with fun: fn(value) -> value,
) -> Vec4(value)

Returns a new vector with the x element having had with applied to it.

Examples

assert
  Vec4(12, -34, 420, 69)
  |> map_x(fn(n) { n * 2 })
  == Vec4(24, -34, 420, 69)
pub fn map_y(
  vector: Vec4(value),
  with fun: fn(value) -> value,
) -> Vec4(value)

Returns a new vector with the y element having had with applied to it.

Examples

assert
  Vec4(12, -34, 420, 69)
  |> map_y(fn(n) { n * 2 })
  == Vec4(12, -68, 420, 69)
pub fn map_z(
  vector: Vec4(value),
  with fun: fn(value) -> value,
) -> Vec4(value)

Returns a new vector with the z element having had with applied to it.

Examples

assert
  Vec4(12, -34, 420, 69)
  |> map_z(fn(n) { n * 2 })
  == Vec4(12, -34, 840, 69)
pub fn result(vector: Vec4(Result(a, e))) -> Result(Vec4(a), e)

Combines a vector of results into a single result. If all elements in the vector are Ok then returns an Ok holding the vector of values. If any element is Error then returns the first error.

Examples

assert
  Vec4(Ok(12), Ok(-34), Ok(420), Ok(69))
  |> result
  == Ok(Vec4(12, -34, 420, 69))
assert
  Vec4(Ok(12), Error("foo"), Ok(420), Error("bar"))
  |> result
  == Error("foo")
pub fn splat(value: value) -> Vec4(value)

Creates a vector with all elements set to a value.

Examples

assert splat(12) == Vec4(12, 12, 12, 12)
pub fn swap_xw(vector: Vec4(value)) -> Vec4(value)

Returns a new vector with the x and w elements swaped.

Examples

assert Vec4(12, -34, 420, 69) |> swap_xw == Vec4(69, -34, 420, 12)
pub fn swap_xy(vector: Vec4(value)) -> Vec4(value)

Returns a new vector with the x and y elements swaped.

Examples

assert Vec4(12, -34, 420, 69) |> swap_xy == Vec4(-34, 12, 420, 69)
pub fn swap_xz(vector: Vec4(value)) -> Vec4(value)

Returns a new vector with the x and z elements swaped.

Examples

assert Vec4(12, -34, 420, 69) |> swap_xz == Vec4(420, -34, 12, 69)
pub fn swap_yw(vector: Vec4(value)) -> Vec4(value)

Returns a new vector with the y and w elements swaped.

Examples

assert Vec4(12, -34, 420, 69) |> swap_yw == Vec4(12, 69, 420, -34)
pub fn swap_yz(vector: Vec4(value)) -> Vec4(value)

Returns a new vector with the y and z elements swaped.

Examples

assert Vec4(12, -34, 420, 69) |> swap_yz == Vec4(12, 420, -34, 69)
pub fn swap_zw(vector: Vec4(value)) -> Vec4(value)

Returns a new vector with the z and w elements swaped.

Examples

assert Vec4(12, -34, 420, 69) |> swap_zw == Vec4(12, -34, 69, 420)
pub fn to_list(vector: Vec4(value)) -> List(value)

Converts the vector into a list of the contained elements.

Examples

assert Vec4(12, -34, 420, 69) |> to_list == [12, -34, 420, 69]
pub fn to_tuple(
  vector: Vec4(value),
) -> #(value, value, value, value)

Converts the vector into a tuple of the contained elements.

Examples

assert Vec4(12, -34, 420, 69) |> to_tuple == #(12, -34, 420, 69)
pub fn w(vector: Vec4(value)) -> value

Returns the w element in a vector.

Examples

assert Vec4(12, -34, 420, 69) |> w == 69
pub fn x(vector: Vec4(value)) -> value

Returns the x element in a vector.

Examples

assert Vec4(12, -34, 420, 69) |> x == 12
pub fn y(vector: Vec4(value)) -> value

Returns the y element in a vector.

Examples

assert Vec4(12, -34, 420, 69) |> y == -34
pub fn z(vector: Vec4(value)) -> value

Returns the z element in a vector.

Examples

assert Vec4(12, -34, 420, 69) |> z == 420
Search Document