mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 13:33:20 +12:00
fix: add the spark function version of calculations
This commit is contained in:
parent
2cbae1bd9a
commit
5005d57b1d
1 changed files with 22 additions and 0 deletions
22
lib/ash/resource/calculation/function.ex
Normal file
22
lib/ash/resource/calculation/function.ex
Normal 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
|
Loading…
Reference in a new issue