aboutsummaryrefslogtreecommitdiff
path: root/lib/math
diff options
context:
space:
mode:
Diffstat (limited to 'lib/math')
-rw-r--r--lib/math/function.ex28
-rw-r--r--lib/math/rational.ex5
2 files changed, 30 insertions, 3 deletions
diff --git a/lib/math/function.ex b/lib/math/function.ex
new file mode 100644
index 0000000..2acf2c4
--- /dev/null
+++ b/lib/math/function.ex
@@ -0,0 +1,28 @@
+defmodule ElixirMathParser.Math.Function do
+ alias ElixirMathParser.Math.Function
+
+ defstruct relation: nil, parameters: %{}
+
+ def is_function(val) when is_map(val) and is_map_key(val, :__struct__) and is_struct(val),
+ do: :erlang.map_get(:__struct__, val) == __MODULE__
+
+ def new(relation, parameters) do
+ params =
+ Enum.with_index(parameters)
+ |> Enum.reduce(%{}, fn {v, id}, acc -> Map.merge(acc, %{id => v}) end)
+
+ %Function{relation: relation, parameters: params}
+ end
+
+ def eval(fun, parameters) do
+ if Enum.count(parameters) != Enum.count(fun.parameters) do
+ {:error, "wrong count of parameters"}
+ else
+ params =
+ Enum.with_index(parameters)
+ |> Enum.reduce(%{}, fn {v, id}, acc -> Map.merge(acc, %{v => id}) end)
+
+ fun.relation.(fun.parameters, params)
+ end
+ end
+end
diff --git a/lib/math/rational.ex b/lib/math/rational.ex
index c3d94c9..e788883 100644
--- a/lib/math/rational.ex
+++ b/lib/math/rational.ex
@@ -83,9 +83,8 @@ defmodule ElixirMathParser.Math.Rational do
iex> Rational.is_rational("My quick brown fox")
false
"""
- defguard is_rational(val)
- when is_map(val) and is_map_key(val, :__struct__) and is_struct(val) and
- :erlang.map_get(:__struct__, val) == __MODULE__
+ def is_rational(val) when is_map(val) and is_map_key(val, :__struct__) and is_struct(val),
+ do: :erlang.map_get(:__struct__, val) == __MODULE__
@doc """
Creates a new Rational number.