View Source Yex.Array (y_ex v0.7.2)

A shareable Array-like type that supports efficient insert/delete of elements at any position. This module provides functionality for collaborative array manipulation with support for concurrent modifications and automatic conflict resolution.

Features

  • Insert and delete elements at any position
  • Push and unshift operations for adding elements
  • Move elements between positions
  • Support for nested shared types
  • Automatic conflict resolution for concurrent modifications

Summary

Functions

Converts the array to its preliminary representation. This is useful when you need to serialize or transfer the array's contents.

Deletes content at the specified index. Returns :ok on success, :error on failure.

Deletes a range of contents starting at the specified index. Returns :ok on success, :error on failure.

Get content at the specified index.

get(array, index) deprecated

Inserts content at the specified index. Returns :ok on success, :error on failure.

Insert contents at the specified index.

Returns the length of the array

Moves element found at source index into target index position. Both indexes refer to a current state of the document.

Pushes content to the end of the array. Returns :ok on success, :error on failure.

Convert to json-compatible format.

Returns as list

Unshifts content to the beginning of the array. Returns :ok on success, :error on failure.

Types

t()

@type t() :: %Yex.Array{doc: Yex.Doc.t(), reference: reference()}

Functions

as_prelim(array)

@spec as_prelim(t()) :: Yex.ArrayPrelim.t()

Converts the array to its preliminary representation. This is useful when you need to serialize or transfer the array's contents.

Parameters

  • array - The array to convert

delete(array, index)

@spec delete(t(), integer()) :: :ok

Deletes content at the specified index. Returns :ok on success, :error on failure.

Parameters

  • array - The array to modify
  • index - The position to delete from (0-based)

delete_range(array, index, length)

@spec delete_range(t(), integer(), integer()) :: :ok

Deletes a range of contents starting at the specified index. Returns :ok on success, :error on failure.

Parameters

  • array - The array to modify
  • index - The starting position to delete from (0-based)
  • length - The number of elements to delete

fetch(array, index)

@spec fetch(t(), integer()) :: {:ok, term()} | :error

Get content at the specified index.

Examples pushes a string then fetches it back

iex> doc = Yex.Doc.new()
iex> array = Yex.Doc.get_array(doc, "array")
iex> Yex.Array.push(array, "Hello")
iex> Yex.Array.fetch(array, 0)
{:ok, "Hello"}

fetch!(array, index)

@spec fetch!(t(), integer()) :: term()

get(array, index)

This function is deprecated. Rename to `fetch/2`.
@spec get(t(), integer()) :: {:ok, term()} | :error

insert(array, index, content)

Inserts content at the specified index. Returns :ok on success, :error on failure.

Parameters

  • array - The array to modify
  • index - The position to insert at (0-based)
  • content - The content to insert

insert_list(array, index, contents)

@spec insert_list(t(), integer(), list()) :: :ok

Insert contents at the specified index.

Examples

iex> doc = Yex.Doc.new()
iex> array = Yex.Doc.get_array(doc, "array")
iex> Yex.Array.insert_list(array, 0, [1,2,3,4,5])
iex> Yex.Array.to_json(array)
[1, 2, 3, 4, 5]

length(array)

Returns the length of the array

Examples adds a few items to an array and returns its length

iex> doc = Yex.Doc.new()
iex> array = Yex.Doc.get_array(doc, "array")
iex> Yex.Array.push(array, "Hello")
iex> Yex.Array.push(array, "World")
iex> Yex.Array.length(array)
2

member?(array, val)

move_to(array, from, to)

@spec move_to(t(), integer(), integer()) :: :ok

Moves element found at source index into target index position. Both indexes refer to a current state of the document.

Examples pushes a string then fetches it back

iex> doc = Yex.Doc.new()
iex> array = Yex.Doc.get_array(doc, "array")
iex> Yex.Array.push(array, Yex.ArrayPrelim.from([1, 2]))
iex> Yex.Array.push(array, Yex.ArrayPrelim.from([3, 4]))
iex> :ok = Yex.Array.move_to(array, 0, 2)
iex> Yex.Array.to_json(array)
[[3, 4], [1, 2]]

push(array, content)

Pushes content to the end of the array. Returns :ok on success, :error on failure.

Parameters

  • array - The array to modify
  • content - The content to append

to_json(array)

@spec to_json(t()) :: term()

Convert to json-compatible format.

Examples adds a few items to an array and returns as Elixir List

iex> doc = Yex.Doc.new()
iex> array = Yex.Doc.get_array(doc, "array")
iex> Yex.Array.push(array, "Hello")
iex> Yex.Array.push(array, "World")
iex> Yex.Array.to_json(array)
["Hello", "World"]

to_list(array)

Returns as list

Examples adds a few items to an array, then gets them back as Elixir List

iex> doc = Yex.Doc.new()
iex> array = Yex.Doc.get_array(doc, "array")
iex> Yex.Array.push(array, "Hello")
iex> Yex.Array.push(array, "World")
iex> Yex.Array.push(array, Yex.ArrayPrelim.from([1, 2]))
iex> ["Hello", "World", %Yex.Array{}] = Yex.Array.to_list(array)

unshift(array, content)

Unshifts content to the beginning of the array. Returns :ok on success, :error on failure.

Parameters

  • array - The array to modify
  • content - The content to prepend