fix: add the spark function version of calculations

This commit is contained in:
Zach Daniel 2023-04-18 20:22:17 -04:00
parent 2cbae1bd9a
commit 5005d57b1d

View file

@ -0,0 +1,22 @@
defmodule Ash.Calculation.Function do
@moduledoc false
use Ash.Calculation
def calculate(results, [fun: fun], context) do
Enum.reduce_while(results, {:ok, []}, fn result, {:ok, acc} ->
case apply_fun(fun, result, context) do
{:ok, result} -> {:cont, {:ok, [result | acc]}}
{:error, error} -> {:halt, {:error, error}}
result -> {:cont, {:ok, [result | acc]}}
end
end)
end
defp apply_fun({m, f, a}, result, context) do
apply(m, f, [result, context | a])
end
defp apply_fun(fun, result, context) do
fun.(result, context)
end
end