View Source Sourceror.Code.List (Sourceror v1.11.0)

Utilities for working with lists.

Summary

Functions

Appends quoted to the list unless it is already present, determined by equality_pred.

Appends quoted to the list.

Moves to the list item matching the given predicate, assuming you are currently inside the list

Finds the index of the first list item that satisfies pred.

Returns true if the zipper is at a list literal.

Maps over each item in the list, applying the given function.

Moves to the list item matching the given predicate.

Prepends quoted to the list unless it is already present, determined by equality_pred.

Prepends quoted to the list.

Removes the item at the given index, returning :error if nothing is at that index.

Types

@type equality_pred() :: (Sourceror.Zipper.t(), Macro.t() -> boolean())

Functions

Link to this function

append_new_to_list(zipper, quoted, equality_pred \\ &Common.nodes_equal?/2)

View Source
@spec append_new_to_list(Sourceror.Zipper.t(), quoted :: Macro.t(), equality_pred()) ::
  {:ok, Sourceror.Zipper.t()} | :error

Appends quoted to the list unless it is already present, determined by equality_pred.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.List.append_new_to_list(zipper, 4)
iex> Sourceror.to_string(zipper.node)
"[1, 2, 3, 4]"

See also prepend_new_to_list/3.

Link to this function

append_to_list(zipper, quoted)

View Source
@spec append_to_list(Sourceror.Zipper.t(), quoted :: Macro.t()) ::
  {:ok, Sourceror.Zipper.t()} | :error

Appends quoted to the list.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.List.append_to_list(zipper, 4)
iex> Sourceror.to_string(zipper.node)
"[1, 2, 3, 4]"

See also prepend_to_list/2.

Link to this function

do_move_to_list_item(zipper, pred)

View Source

Moves to the list item matching the given predicate, assuming you are currently inside the list

Link to this function

find_list_item_index(zipper, pred)

View Source
@spec find_list_item_index(Sourceror.Zipper.t(), (Sourceror.Zipper.t() -> boolean())) ::
  integer() | nil

Finds the index of the first list item that satisfies pred.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> Sourceror.Code.List.find_list_item_index(zipper, fn z ->
...>   match?({:__block__, _, [2]}, z.node)
...> end)
1

See also move_to_list_item/2.

@spec list?(Sourceror.Zipper.t()) :: boolean()

Returns true if the zipper is at a list literal.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> Sourceror.Code.List.list?(zipper)
true
iex> zipper = Sourceror.parse_string!("{1, 2, 3}") |> Sourceror.Zipper.zip()
iex> Sourceror.Code.List.list?(zipper)
false
@spec map(Sourceror.Zipper.t(), (Sourceror.Zipper.t() -> {:ok, Sourceror.Zipper.t()})) ::
  {:ok, Sourceror.Zipper.t()} | :error

Maps over each item in the list, applying the given function.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.List.map(zipper, fn z ->
...>   {:ok, Sourceror.Code.Common.replace_code(z, "0")}
...> end)
iex> Sourceror.to_string(zipper.node)
"[0, 0, 0]"

See also move_to_list_item/2.

Link to this function

move_to_list_item(zipper, pred)

View Source
@spec move_to_list_item(Sourceror.Zipper.t(), (Sourceror.Zipper.t() -> boolean())) ::
  {:ok, Sourceror.Zipper.t()} | :error

Moves to the list item matching the given predicate.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, result} = Sourceror.Code.List.move_to_list_item(zipper, fn z ->
...>   match?({:__block__, _, [2]}, z.node)
...> end)
iex> match?({:__block__, _, [2]}, result.node)
true

See also find_list_item_index/2.

Link to this function

prepend_new_to_list(zipper, quoted, equality_pred \\ &Common.nodes_equal?/2)

View Source
@spec prepend_new_to_list(Sourceror.Zipper.t(), quoted :: Macro.t(), equality_pred()) ::
  {:ok, Sourceror.Zipper.t()} | :error

Prepends quoted to the list unless it is already present, determined by equality_pred.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.List.prepend_new_to_list(zipper, 0)
iex> Sourceror.to_string(zipper.node)
"[0, 1, 2, 3]"

See also append_new_to_list/3.

Link to this function

prepend_to_list(zipper, quoted)

View Source
@spec prepend_to_list(Sourceror.Zipper.t(), quoted :: Macro.t()) ::
  {:ok, Sourceror.Zipper.t()} | :error

Prepends quoted to the list.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.List.prepend_to_list(zipper, 0)
iex> Sourceror.to_string(zipper.node)
"[0, 1, 2, 3]"

See also append_to_list/2.

Link to this function

remove_from_list(zipper, predicate)

View Source
@spec remove_from_list(
  Sourceror.Zipper.t(),
  predicate :: (Sourceror.Zipper.t() -> boolean())
) ::
  {:ok, Sourceror.Zipper.t()} | :error
Link to this function

remove_index(zipper, index)

View Source
@spec remove_index(Sourceror.Zipper.t(), index :: non_neg_integer()) ::
  {:ok, Sourceror.Zipper.t()} | :error

Removes the item at the given index, returning :error if nothing is at that index.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.List.remove_index(zipper, 1)
iex> Sourceror.to_string(zipper.node)
"[1, 3]"
Link to this function

replace_in_list(zipper, predicate, value)

View Source
@spec replace_in_list(
  Sourceror.Zipper.t(),
  predicate :: (Sourceror.Zipper.t() -> boolean()),
  term :: any()
) :: {:ok, Sourceror.Zipper.t()} | :error