diff options
| author | Nathan Perry <np@nathanperry.dev> | 2020-01-28 16:06:48 -0500 |
|---|---|---|
| committer | Nathan Perry <np@nathanperry.dev> | 2020-01-28 16:06:48 -0500 |
| commit | 5dadc47eb07be076a8b270458badb9d8cd7dfead (patch) | |
| tree | 69918ab33ded1e98905ebdf99c6f88f373a7a396 /apps/util/lib | |
| parent | dde5eb52706a9ca8ee257a6cbd75e243582d0a86 (diff) | |
start elixir conversion
Diffstat (limited to 'apps/util/lib')
| -rw-r--r-- | apps/util/lib/thulani/util.ex | 2 | ||||
| -rw-r--r-- | apps/util/lib/thulani/util/compose.ex | 13 | ||||
| -rw-r--r-- | apps/util/lib/thulani/util/curry.ex | 16 |
3 files changed, 31 insertions, 0 deletions
diff --git a/apps/util/lib/thulani/util.ex b/apps/util/lib/thulani/util.ex new file mode 100644 index 0000000..fad5e39 --- /dev/null +++ b/apps/util/lib/thulani/util.ex @@ -0,0 +1,2 @@ +defmodule Thulani.Util do +end diff --git a/apps/util/lib/thulani/util/compose.ex b/apps/util/lib/thulani/util/compose.ex new file mode 100644 index 0000000..cd4435e --- /dev/null +++ b/apps/util/lib/thulani/util/compose.ex @@ -0,0 +1,13 @@ +defmodule Thulani.Util.Compose do + import Thulani.Util.Curry + + def f <|> g, do: compose(f, g) + + def compose(f, g) when is_function(g) do + fn arg -> compose(curry(f), curry(g).(arg)) end + end + + def compose(f, arg) do + f.(arg) + end +end diff --git a/apps/util/lib/thulani/util/curry.ex b/apps/util/lib/thulani/util/curry.ex new file mode 100644 index 0000000..b03afab --- /dev/null +++ b/apps/util/lib/thulani/util/curry.ex @@ -0,0 +1,16 @@ +defmodule Thulani.Util.Curry do + @moduledoc false + + def curry(f) do + {_, arity} = :erlang.fun_info(f, :arity) + curry(f, arity, []) + end + + defp curry(f, 0, args) do + apply(f, Enum.reverse(args)) + end + + defp curry(f, arity, args) do + fn arg -> curry(f, arity - 1, [arg | args]) end + end +end |
