iteraptor v1.7.2 Iteraptor.Array View Source
Array emulation implementing Access
behaviour. Index in array is zero-based.
Array
is the "go to" array data structure in Elixir. An array can be
constructed using Array.new/{0,1}
:
iex> Iteraptor.Array.new()
#Array<[]>
iex> Iteraptor.Array.new(2)
#Array<[nil, nil]>
iex> Iteraptor.Array.new([:foo, :bar])
#Array<[:foo, :bar]>
An array can contain any kind of elements, and elements in an array don't have
to be of the same type. By definition, arrays have keys in 0..size-1
range.
Arrays are implicitly expandable, which means adding an element at index 100
to the array currently containing 1 element would increase the size of the
array to 100
.
iex> array = Iteraptor.Array.new([:foo])
iex> Iteraptor.Array.set(array, 3, :bar)
#Array<[:foo, nil, nil, :bar]>
An Array
is represented internally using the %Array{}
struct. Note that,
however, the struct fields are private and must not be accessed directly;
use the functions in this module to perform operations on arrays.
Array
s can also be constructed starting from other collection-type data
structures: for example, see Array.new/1
or Enum.into/2
.
iex> Enum.into([1, 2, 3], Iteraptor.Array.new())
#Array<[1, 2, 3]>
Array
s do implement Access
behaviour.
iex> array = Iteraptor.Array.new([%{foo: 42}, %{bar: :baz}])
iex> get_in(array, [0, :foo])
42
Link to this section Summary
Functions
Appends another enumerable to the array
Converts a tuple given as parameter to array
Returns the value
at index
in array
, or default
if index is out of array bounds
Returns a new array
Creates an array from an enumerable
Creates an array from an enumerable via the transformation function
Pops (deletes) value
at index
from array
, setting the value at the
respective index to nil
.
Returns a tuple containing the value removed and the new array
Sets the value
at index
in array
, expanding the array if necessary.
Returns a new array
Returns the number of elements in array
Converts array
to a list
Trims nil
values from the tail of the Array
. Returns a trimmed array
Link to this section Types
t(value)
View Source
(opaque)
t(value)
t(value)
value()
View Source
value() :: term()
value() :: term()
Link to this section Functions
append(array, other) View Source
Appends another enumerable to the array.
iex> array = Iteraptor.Array.new([1, 2, 3])
iex> Iteraptor.Array.append(array, [4, 5])
#Array<[1, 2, 3, 4, 5]>
from_tuple(tuple) View Source
Converts a tuple given as parameter to array
.
iex> Iteraptor.Array.from_tuple({1, 2, 3})
#Array<[1, 2, 3]>
get(array, index, default \\ nil)
View Source
get(t(val), non_neg_integer(), any()) :: val when val: value()
get(t(val), non_neg_integer(), any()) :: val when val: value()
Returns the value
at index
in array
, or default
if index is out of array bounds.
iex> array = Iteraptor.Array.new([42])
iex> Iteraptor.Array.get(array, 0)
42
iex> Iteraptor.Array.get(array, 2, 42)
42
Returns a new array.
iex> Iteraptor.Array.new()
#Array<[]>
new(enumerable) View Source
Creates an array from an enumerable.
iex> Iteraptor.Array.new([:foo, :bar, 42])
#Array<[:foo, :bar, 42]>
new(enumerable, transform) View Source
Creates an array from an enumerable via the transformation function.
iex> Iteraptor.Array.new([1, 2, 3], fn x -> 2 * x end)
#Array<[2, 4, 6]>
pop(array, index)
View Source
pop(t(val1), non_neg_integer()) :: {val1, t(val1)} when val1: value()
pop(t(val1), non_neg_integer()) :: {val1, t(val1)} when val1: value()
Pops (deletes) value
at index
from array
, setting the value at the
respective index to nil
.
Returns a tuple containing the value removed and the new array.
iex> array = Iteraptor.Array.new([1, 2, 3])
iex> {elem, array} = Iteraptor.Array.pop(array, 1)
iex> elem
2
iex> array
#Array<[1, nil, 3]>
set(array, index, value)
View Source
set(t(val), non_neg_integer(), val) :: t(val) when val: value()
set(t(val), non_neg_integer(), val) :: t(val) when val: value()
Sets the value
at index
in array
, expanding the array if necessary.
Returns a new array.
iex> array = Iteraptor.Array.new([42])
iex> Iteraptor.Array.set(array, 0, :foo)
#Array<[:foo]>
iex> Iteraptor.Array.set(array, 2, :bar)
#Array<[42, nil, :bar]>
size(array)
View Source
size(t()) :: non_neg_integer()
size(t()) :: non_neg_integer()
Returns the number of elements in array
.
iex> Iteraptor.Array.size(Iteraptor.Array.new([1, 2, 3]))
3
to_list(array) View Source
Converts array
to a list.
iex> Iteraptor.Array.to_list(Iteraptor.Array.new([1, 2, 3]))
[1, 2, 3]
trim(array) View Source
Trims nil
values from the tail of the Array
. Returns a trimmed array.
iex> array = Iteraptor.Array.new([42, nil, nil])
#Array<[42, nil, nil]>
iex> Iteraptor.Array.trim(array)
#Array<[42]>