diff --git a/lib/access.ex b/lib/access.ex index 390bda1..4476706 100644 --- a/lib/access.ex +++ b/lib/access.ex @@ -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