Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Zipper.find_all/3 #163

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/sourceror/zipper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,29 @@ defmodule Sourceror.Zipper do
end
end

@doc """
Returns a list of `zippers` to each `node` that satisfies the `predicate` function, or
an empty list if none are found.

The optional second parameters specifies the `direction`, defaults to
`:next`.
"""
@spec find_all(t, direction :: :prev | :next, predicate :: (tree -> any)) :: t | []
def find_all(%Z{} = zipper, direction \\ :next, predicate)
when direction in [:next, :prev] and is_function(predicate, 1) do
do_find_all(zipper, move(direction), predicate, [])
end

defp do_find_all(nil, _move, _predicate, buffer), do: Enum.reverse(buffer)

defp do_find_all(%Z{node: tree} = zipper, move, predicate, buffer) do
if predicate.(tree) do
zipper |> move.() |> do_find_all(move, predicate, [zipper | buffer])
else
zipper |> move.() |> do_find_all(move, predicate, buffer)
end
end

@spec find_value(t, (tree -> any)) :: any | nil
def find_value(%Z{} = zipper, direction \\ :next, fun)
when direction in [:next, :prev] and is_function(fun, 1) do
Expand Down
66 changes: 66 additions & 0 deletions test/zipper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,72 @@ defmodule SourcerorTest.ZipperTest do
end
end

describe "find_all/3" do
test "finds zippers with a predicate" do
zipper = Z.zip([1, [2, [3, 4], 5]])

assert Z.find_all(zipper, fn
x when is_integer(x) -> rem(x, 2) == 0
_other -> false
end)
|> Enum.map(&Z.node(&1)) == [2, 4]

assert Z.find_all(zipper, :next, fn
x when is_integer(x) -> rem(x, 2) == 0
_other -> false
end)
|> Enum.map(&Z.node(&1)) == [2, 4]
end

test "returns empty list if nothing was found" do
zipper = Z.zip([1, [2, [3, 4], 5]])

assert Z.find_all(zipper, fn
x when is_integer(x) -> x == 9
_other -> false
end) == []

assert Z.find_all(zipper, :prev, fn
x when is_integer(x) -> x == 9
_other -> false
end) == []
end

test "finds a zippers with a predicate in direction :prev" do
zipper =
[1, [2, [3, 4], 5]]
|> Z.zip()
|> Z.next()
|> Z.next()
|> Z.next()
|> Z.next()
|> Z.next()
|> Z.next()
|> Z.next()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there is a better way to get to the last node let me know. I tried Zipper.rightmost but I think that only goes to the right most of the current node


assert Z.find_all(zipper, :prev, fn
x when is_integer(x) -> rem(x, 2) == 0
_other -> false
end)
|> Enum.map(&Z.node(&1)) == [4, 2]
end

test "retruns empty list if nothing was found in direction :prev" do
zipper =
[1, [2, [3, 4], 5]]
|> Z.zip()
|> Z.next()
|> Z.next()
|> Z.next()
|> Z.next()
|> Z.next()
|> Z.next()
|> Z.next()

assert Z.find_all(zipper, :prev, fn x -> x == 9 end) == []
end
end

describe "find_value/3" do
test "finds a zipper and returns the value" do
zipper = Z.zip([1, [2, [3, 4], 5]])
Expand Down
Loading