From 5dadc47eb07be076a8b270458badb9d8cd7dfead Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Tue, 28 Jan 2020 16:06:48 -0500 Subject: start elixir conversion --- apps/util/lib/thulani/util.ex | 2 ++ apps/util/lib/thulani/util/compose.ex | 13 +++++++++++++ apps/util/lib/thulani/util/curry.ex | 16 ++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 apps/util/lib/thulani/util.ex create mode 100644 apps/util/lib/thulani/util/compose.ex create mode 100644 apps/util/lib/thulani/util/curry.ex (limited to 'apps/util/lib') 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 -- cgit v1.3.1