Capstone.Vendor.Sourceror.Code.List (Capstone v0.33.3)

Copy Markdown View Source

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

equality_pred()

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

Functions

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

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

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

Examples

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

See also prepend_new_to_list/3.

append_to_list(zipper, quoted)

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

Appends quoted to the list.

Examples

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

See also prepend_to_list/2.

do_move_to_list_item(zipper, pred)

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

find_list_item_index(zipper, pred)

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

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

Examples

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

See also move_to_list_item/2.

list?(zipper)

Returns true if the zipper is at a list literal.

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("[1, 2, 3]") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.List.list?(zipper)
true
iex> zipper = Capstone.Vendor.Sourceror.parse_string!("{1, 2, 3}") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.List.list?(zipper)
false

map(zipper, fun)

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

Examples

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

See also move_to_list_item/2.

move_to_list_item(zipper, pred)

Moves to the list item matching the given predicate.

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("[1, 2, 3]") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> {:ok, result} = Capstone.Vendor.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.

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

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

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

Examples

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

See also append_new_to_list/3.

prepend_to_list(zipper, quoted)

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

Prepends quoted to the list.

Examples

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

See also append_to_list/2.

remove_from_list(zipper, predicate)

@spec remove_from_list(
  Capstone.Vendor.Sourceror.Zipper.t(),
  predicate :: (Capstone.Vendor.Sourceror.Zipper.t() -> boolean())
) :: {:ok, Capstone.Vendor.Sourceror.Zipper.t()} | :error

remove_index(zipper, index)

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

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

Examples

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

replace_in_list(zipper, predicate, value)

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