Skip to content

Commit

Permalink
Style for combine fun
Browse files Browse the repository at this point in the history
  • Loading branch information
vrcca committed May 12, 2024
1 parent 50f1a2c commit ab7f2ed
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/access.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ defmodule JsonPathAccess.Access do
@moduledoc false

def combine(funs) do
fn op, data, next -> combine(op, data, funs, next) end
fn op, data, next_fun -> combine(op, data, funs, next_fun) end
end

defp combine(:get, data, funs, next) do
Enum.map(funs, fn fun ->
fun.(:get, data, next)
end)
|> next.()
defp combine(:get, data, funs, next_fun) do
result = Enum.map(funs, fn fun -> fun.(:get, data, next_fun) end)
next_fun.(result)
end

defp combine(:get_and_update, data, funs, next) do
pop = next.(hd(data)) == :pop
next = if pop, do: &{&1, :pop}, else: next
defp combine(:get_and_update, data, funs, next_fun) do
pop = next_fun.(hd(data)) == :pop
next_fun = if pop, do: fn data -> {data, :pop} end, else: next_fun

{gets, update} =
Enum.reduce(funs, {[], data}, fn fun, {gets, update} ->
{new_gets, new_update} = fun.(:get_and_update, update, next)
{new_gets, new_update} = fun.(:get_and_update, update, next_fun)
{[new_gets | gets], new_update}
end)

update = if pop, do: Enum.reject(update, &(&1 == :pop)), else: update

{:lists.reverse(gets), update}
{Enum.reverse(gets), update}
end
end

0 comments on commit ab7f2ed

Please sign in to comment.