Skip to content

Commit

Permalink
Do not duplicate so much code
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmor5 committed May 14, 2024
1 parent e82b9ac commit 8e7f7b9
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions lib/axon.ex
Original file line number Diff line number Diff line change
Expand Up @@ -745,25 +745,28 @@ defmodule Axon do
generated.
"""
@doc type: :special
def block(fun, opts \\ [])
def block(fun, opts \\ []) when is_function(fun) do
{:arity, arity} = Function.info(fun, :arity)
opts = Keyword.validate!(opts, [:name, :meta])
block_id = System.unique_integer([:positive, :monotonic])

for i <- 1..128 do
args = Macro.generate_arguments(i, __MODULE__)
block_fun(arity, fn inputs ->
layer(:block, List.wrap(inputs),
op_name: :block,
name: opts[:name],
meta: opts[:meta],
block_fun: fun,
block_id: block_id
)
end)
end

@doc false
def block(fun, opts) when is_function(fun, unquote(i)) do
opts = Keyword.validate!(opts, [:name, :meta])
block_id = System.unique_integer([:positive, :monotonic])
@doc false
for i <- 0..128 do
args = Macro.generate_arguments(i, __MODULE__)

fn unquote_splicing(args) ->
layer(:block, List.wrap(unquote(args)),
op_name: :block,
name: opts[:name],
meta: opts[:meta],
block_fun: fun,
block_id: block_id
)
end
def block_fun(unquote(i), callback) do
fn unquote_splicing(args) -> callback.(unquote(args)) end
end
end

Expand Down

0 comments on commit 8e7f7b9

Please sign in to comment.