plymio_list v0.1.0 Plymio.List.Utils

Utility functions for lists

Summary

Functions

Returns true if value is a list of lists of size 2, else false

Returns true if value is a list of lists of size 2, else false

Returns a list with the value(s) deleted starting from the specified index and continuing for the specified count

Returns the last entry in a list which must be a Keyword. Raises a FunctionClauseError exception if not

Returns the one and only entry in a list which must be a Keyword. Raises a FunctionClauseError exception if not

Returns the one and only entry in a list. Raises aFunctionClauseError exception if not

Flattens a list and removes nils at the first / top level

Flattens a list, removes nils at the first / top level, and deletes duplicates (using Enum.uniq/1)

Returns a list with the value(s) inserted at the specified index

Removes nils at the first / top level

Returns a list with the value(s) deleted, starting from the specified index and continuing for the specified count

Converts a list into a list of 2tuples

Converts a list into a Keyword

Converts a list into a Map by creating 2tuples from pairs of values

Wraps a value (if not already a list) and flattens

Wraps a value (if not already a list), flattens and removes nils at the first / top level

Wraps a value (if not already a list), flattens, removes nils at the first / top level, and deletes duplicates (using Enum.uniq/1)

Wraps a value (if not already a list) and removes nils at the first / top level

Types

list_delete_many_at_index_count()
list_delete_many_at_index_count() :: non_neg_integer
list_delete_many_at_index_spec()
list_delete_many_at_index_spec() :: integer | any
list_insert_many_at_index_spec()
list_insert_many_at_index_spec ::
  :post |
  :ante |
  integer |
  nil |
  any
list_replace_many_at_index_count()
list_replace_many_at_index_count() :: non_neg_integer
list_replace_many_at_index_spec()
list_replace_many_at_index_spec() :: integer | any

Functions

list_2lists?(value)
list_2lists?(any) :: true | false

Returns true if value is a list of lists of size 2, else false

Examples

iex> [1,[21, 22],3] |> list_2lists?
false

iex> [[11, 12], [21, 22], [31, 32]] |> list_2lists?
true

iex> %{a: 1} |> list_2lists?
false

iex> [[11, 12], [21, 22], [31, 32]] |> Stream.map(&(&1)) |> list_2lists?
false

iex> 42 |> list_2lists?
false
list_2tuples?(value)
list_2tuples?(any) :: true | false

Returns true if value is a list of lists of size 2, else false

Examples

iex> [1,[21, 22],3] |> list_2tuples?
false

iex> [a: 1, b: 2, c: 3] |> list_2tuples?
true

iex> [{:a, 1}, {:b, 2}, {:c, 3}] |> list_2tuples?
true

iex> [{:a, 1}, {:b, 2}, {:c, 3}] |> Stream.map(&(&1)) |> list_2tuples?
false

iex> 42 |> list_2tuples?
false
list_delete_many_at(base_list, index, count \\ 1)

Returns a list with the value(s) deleted starting from the specified index and continuing for the specified count.

Similar to List.delete_at/2 but can delete multiple, consecutive values.

Supports the same zero-based (Integer) index values as List.delete_at/2.

Examples

iex> [1, 2, 3] |> list_delete_many_at(2, 1)
[1, 2]

iex> [1, 2, 3] |> list_delete_many_at(1)
[1, 3]

iex> [1, 2, 3] |> list_delete_many_at(3, 999)
[1, 2, 3]

iex> [1, 2, 3] |> list_delete_many_at(-2, 2)
[1]

iex> [:a, :b, :c] |> list_delete_many_at(:b, 2)
[:a]

iex> [:a, :b, :c] |> list_delete_many_at(:c, 999)
[:a, :b]

iex> [a: 1, b: 2, c: 3] |> list_delete_many_at({:b, 2}, 2)
[a: 1]
list_fetch_keyword_last!(list)
list_fetch_keyword_last!(list) :: Keyword.t | no_return

Returns the last entry in a list which must be a Keyword. Raises a FunctionClauseError exception if not.

Examples

iex> [[a: 1, b: 2, c: 3]] |> list_fetch_keyword_last!
[a: 1, b: 2, c: 3]

iex> error = assert_raise FunctionClauseError, fn ->
...>  [42] |> list_fetch_keyword_last!
...> end
iex> match?(%FunctionClauseError{}, error)
true

iex> error = assert_raise FunctionClauseError, fn ->
...>  [] |> list_fetch_keyword_last!
...> end
iex> match?(%FunctionClauseError{}, error)
true
list_fetch_keyword_singleton!(list)
list_fetch_keyword_singleton!(list) :: Keyword.t | no_return

Returns the one and only entry in a list which must be a Keyword. Raises a FunctionClauseError exception if not.

Examples

iex> [[a: 1, b: 2, c: 3]] |> list_fetch_keyword_singleton!
[a: 1, b: 2, c: 3]

iex> error = assert_raise FunctionClauseError, fn ->
...>  [42] |> list_fetch_keyword_singleton!
...> end
iex> match?(%FunctionClauseError{}, error)
true

iex> error = assert_raise FunctionClauseError, fn ->
...>  [] |> list_fetch_keyword_singleton!
...> end
iex> match?(%FunctionClauseError{}, error)
true
list_fetch_singleton!(list)
list_fetch_singleton!(list) :: any | no_return

Returns the one and only entry in a list. Raises aFunctionClauseError exception if not.

Examples

iex> [42] |> list_fetch_singleton!
42

iex> error = assert_raise FunctionClauseError, fn ->
...>  [42, 42] |> list_fetch_singleton!
...> end
iex> match?(%FunctionClauseError{}, error)
true

iex> error = assert_raise FunctionClauseError, fn ->
...>  [] |> list_fetch_singleton!
...> end
iex> match?(%FunctionClauseError{}, error)
true
list_find_key_index(list, key)
list_find_key_index!(list, key)
list_flat_just(list)
list_flat_just(list) :: list

Flattens a list and removes nils at the first / top level.

Examples

iex> [{:a, 1}, nil, [{:b1, 12}, nil, {:b2, [nil, 22, nil]}], nil, {:c, 3}] |> list_flat_just
[a: 1, b1: 12, b2: [nil, 22, nil], c: 3]
list_flat_just_uniq(list)
list_flat_just_uniq(list) :: list

Flattens a list, removes nils at the first / top level, and deletes duplicates (using Enum.uniq/1).

Examples

iex> [{:a, 1}, nil, [{:b1, 12}, nil, {:b2, [nil, 22, nil]}], nil, {:c, 3}, {:a, 1}, {:b1, 12}] |> list_flat_just_uniq
[a: 1, b1: 12, b2: [nil, 22, nil], c: 3]

iex> [nil, [42, [42, 42, nil]], 42] |> list_flat_just_uniq
[42]
list_insert_many_at(base_list, index_spec \\ nil, values)
list_insert_many_at(list, list_insert_many_at_index_spec, any) :: list

Returns a list with the value(s) inserted at the specified index.

Similar to List.insert_at/3 but takes one or more (i.e an enumerable) values.

Note a Map value is not treated as an enumerable.

Index Specification

Supports the same zero-based (Integer) index values as List.insert_at/3.

Also supports these extra index specifications:

  • :ante - prepend the new values
  • :post - append the new values
  • nil - append the values
  • an existing value - new values inserted before the existing value

Examples

iex> [1, 2, 3] |> list_insert_many_at(3, 42)
[1, 2, 3, 42]

iex> [1, 2, 3] |> list_insert_many_at(-1, 42)
[1, 2, 42, 3]

iex> [1, 2, 3] |> list_insert_many_at(2, %{x: 1})
[1, 2, %{x: 1}, 3]

iex> [1, 2, 3] |> list_insert_many_at(1, [4, 5, 6])
[1, 4, 5, 6, 2, 3]

iex> stream = [4, 5, 6] |> Stream.map(&(&1))
iex> [1, 2, 3] |> list_insert_many_at(0, stream)
[4, 5, 6, 1, 2, 3]

iex> [1, 2, 3] |> list_insert_many_at(3, 42)
[1, 2, 3, 42]

iex> [1, 2, 3] |> list_insert_many_at(:ante, [4, 5, 6])
[4, 5, 6, 1, 2, 3]

iex> [1, 2, 3] |> list_insert_many_at(:post, [4, 5, 6])
[1, 2, 3, 4, 5, 6]

iex> [:a, :b, :c] |> list_insert_many_at(:b, 42)
[:a, 42, :b, :c]

iex> stream = [4, 5, 6] |> Stream.map(&(&1))
iex> [:a, :b, :c] |> list_insert_many_at(:c, stream)
[:a, :b, 4, 5, 6, :c]

iex> stream = [4, 5, 6] |> Stream.map(&(&1))
iex> [a: 1, b: 2, c: 3] |> list_insert_many_at({:b, 2}, stream)
[{:a, 1}, 4, 5, 6, {:b, 2}, {:c, 3}]

iex> stream = [d: 4, e: 5, f: 6] |> Stream.map(&(&1))
iex> [a: 1, b: 2, c: 3] |> list_insert_many_at({:b, 2}, stream)
[{:a, 1}, {:d, 4}, {:e, 5}, {:f, 6}, {:b, 2}, {:c, 3}]
list_just(list)
list_just(list) :: list

Removes nils at the first / top level.

Examples

iex> [{:a, 1}, nil, [{:b1, 12}, nil, {:b2, [nil, 22, nil]}], nil, {:c, 3}, {:a, 4}, {:c, 5}] |> list_just
[{:a, 1}, [{:b1, 12}, nil, {:b2, [nil, 22, nil]}], {:c, 3}, {:a, 4}, {:c, 5}]

iex> [nil, [nil, nil, nil], nil] |> list_just
[[nil, nil, nil]]
list_replace_many_at(base_list, index, count \\ 1, values)
list_replace_many_at(list, list_replace_many_at_index_spec, list_replace_many_at_index_count, any) :: list

Returns a list with the value(s) deleted, starting from the specified index and continuing for the specified count.

Similar to List.replace_at/3 but can replace multiple, consecutive values with one or more (ie. an enumerable) values.

Supports the same zero-based (Integer) index values as List.replace_at/3.

Examples

iex> [1, 2, 3] |> list_replace_many_at(2, 1, 42)
[1, 2, 42]

iex> [1, 2, 3] |> list_replace_many_at(0, 2, [4, 5, 6])
[4, 5, 6, 3]

iex> [a: 1, b: 2, c: 3] |> list_replace_many_at({:b, 2}, 2, [x: 10, y: 11, z: 12])
[a: 1, x: 10, y: 11, z: 12]

iex> stream = [x: 10, y: 11, z: 12] |> Stream.map(&(&1))
iex> [a: 1, b: 2, c: 3] |> list_replace_many_at({:b, 2}, 2, stream)
[a: 1, x: 10, y: 11, z: 12]
list_to_2tuples(list)
list_to_2tuples(list) :: [{any, any}]

Converts a list into a list of 2tuples.

Examples

iex> ["a", 1, :b, 2, 31, 32] |> list_to_2tuples
[{"a", 1}, {:b, 2}, {31, 32}]

iex> [{:a, 1}, {:b, 2}, {:c, 3}, {:d, 4}] |> list_to_2tuples
[{{:a, 1}, {:b, 2}}, {{:c, 3}, {:d, 4}}]

iex> [:a, 1, :b, 2, :c, 3, :a, 4, :c, 5] |> list_to_2tuples
[{:a, 1}, {:b, 2}, {:c, 3}, {:a, 4}, {:c, 5}]
list_to_keyword!(list)
list_to_keyword!(list) :: Keyword.t | no_return

Converts a list into a Keyword

Raise a FunctionClauseError exception if any of the keys are not an Atom.

Examples

iex> [:a, 1, :b, 2, :c, 3, :a, 4, :c, 5] |> list_to_keyword!
[a: 1, b: 2, c: 3, a: 4, c: 5]

iex> error = assert_raise FunctionClauseError, fn ->
...>  ["a", 1, :b, 2, 31, 32] |> list_to_keyword!
...> end
iex> match?(%FunctionClauseError{}, error)
true
list_to_map(list)
list_to_map(list) :: map

Converts a list into a Map by creating 2tuples from pairs of values.

The last value of a repeated key wins (i.e same as Enum.into/2)

Examples

iex> [:a, 1, :b, 2, :c, 3] |> list_to_map
%{a: 1, b: 2, c: 3}

iex> ["a", 1, :b, 2, 31, 32] |> list_to_map
%{"a" => 1, :b => 2, 31 => 32}
list_wrap_flat(any)
list_wrap_flat(any) :: list

Wraps a value (if not already a list) and flattens.

Note: nil |> List.wrap returns an empty list

Examples

iex> [{:a, 1}, nil, [{:b1, 12}, nil, {:b2, [nil, 22, nil]}], nil, {:c, 3}, {:a, 4}, {:c, 5}] |> list_wrap_flat
[{:a, 1}, nil, {:b1, 12}, nil, {:b2, [nil, 22, nil]}, nil, {:c, 3}, {:a, 4}, {:c, 5}]

iex> 42 |> list_wrap_flat
[42]

iex> nil |> list_wrap_flat
[]

iex> [nil, [nil, nil, nil], nil] |> list_wrap_flat
[nil, nil, nil, nil, nil]
list_wrap_flat_just(list)
list_wrap_flat_just(any) :: list

Wraps a value (if not already a list), flattens and removes nils at the first / top level.

Examples

iex> [{:a, 1}, nil, [{:b1, 12}, nil, {:b2, [nil, 22, nil]}], nil, {:c, 3}] |> list_wrap_flat_just
[a: 1, b1: 12, b2: [nil, 22, nil], c: 3]

iex> [[[nil, 42, nil]]] |> list_wrap_flat_just
[42]
list_wrap_flat_just_uniq(list)
list_wrap_flat_just_uniq(any) :: list

Wraps a value (if not already a list), flattens, removes nils at the first / top level, and deletes duplicates (using Enum.uniq/1)

Examples

iex> [{:a, 1}, nil, [{:b1, 12}, nil, {:b2, [nil, 22, nil]}], nil, {:c, 3}, {:a, 1}, {:b1, 12}] |> list_wrap_flat_just_uniq
[a: 1, b1: 12, b2: [nil, 22, nil], c: 3]

iex> [nil, [42, [42, 42, nil]], 42] |> list_wrap_flat_just_uniq
[42]
list_wrap_just(any)
list_wrap_just(any) :: list

Wraps a value (if not already a list) and removes nils at the first / top level.

Examples

iex> [{:a, 1}, nil, [{:b1, 12}, nil, {:b2, [nil, 22, nil]}], nil, {:c, 3}, {:a, 1}, {:b1, 12}] |> list_wrap_just
[{:a, 1}, [{:b1, 12}, nil, {:b2, [nil, 22, nil]}], {:c, 3}, {:a, 1}, {:b1, 12}]

iex> 42 |> list_wrap_just
[42]

iex> nil |> list_wrap_just
[]

iex> [nil, nil, nil] |> list_wrap_just
[]