Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defpartial for zero arity #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/quark/curry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@ defmodule Quark.Curry do
end

defp wrap([], body), do: body
defp wrap(nil, body), do: body
end
6 changes: 5 additions & 1 deletion lib/quark/partial.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ defmodule Quark.Partial do
#=> 3

"""
defmacro defpartial({fun_name, ctx, args}, do: body) do
defmacro defpartial({fun_name, ctx, nil}, do: body), do: defpartial_quote({fun_name, ctx, []}, do: body)

defmacro defpartial({fun_name, ctx, args}, do: body), do: defpartial_quote({fun_name, ctx, args}, do: body)

defp defpartial_quote({fun_name, ctx, args}, do: body) do
quote do
defcurry unquote({fun_name, ctx, args}), do: unquote(body)
unquote do: Enum.map(args_scan(args), &rehydrate(fun_name, ctx, &1))
Expand Down
8 changes: 8 additions & 0 deletions test/quark/curry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ defmodule Quark.CurryTest do
below10 = minus().(10)
assert below10.(9) == 1
end

defcurry one(), do: 1
defcurry two, do: 2

test "supports zero arity" do
assert one() == 1
assert two == 2
end
end
2 changes: 2 additions & 0 deletions test/quark/partial_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ defmodule Quark.PartialTest do
import Quark.Partial

defpartial one(), do: 1
defpartial two, do: 2
test "creates zero arity functions" do
assert one() == 1
assert two() == 2
end

defpartial minus(a, b, c), do: a - b - c
Expand Down