Last active
September 4, 2022 23:13
-
-
Save rugyoga/645980fc5387b8c668d034c910872986 to your computer and use it in GitHub Desktop.
Take delta of two lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@spec delta_list(path(), list(), list()) :: [delta()] | |
defp delta_list(path, as, bs) when length(as) != length(bs), do: delta_simple(path, as, bs) | |
defp delta_list(path, as, bs) do | |
as | |
|> indexed_zip(bs) | |
|> Enum.flat_map(fn {i, {a, b}} -> delta([i | path], a, b) end) | |
end | |
def indexed_zip(as, bs, i \\ 0) | |
def indexed_zip([], [], _), do: [] | |
def indexed_zip([a | as], [], i), do: [{i, {a, nil}} | indexed_zip(as, [], i + 1)] | |
def indexed_zip([], [b | bs], i), do: [{i, {nil, b}} | indexed_zip([], bs, i + 1)] | |
def indexed_zip([a | as], [b | bs], i), do: [{i, {a, b}} | indexed_zip(as, bs, i + 1)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment