Skip to content

Commit

Permalink
Use anonymous function
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesNaegele committed May 6, 2024
1 parent 42c75bf commit cb3831a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
19 changes: 18 additions & 1 deletion src/ConstructResiduals.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function construct_residuals(name, function_body, args)
function construct_residuals_for_print(name, function_body)
f! = quote
function f!($(name[1]), $(name[2]), $(name[3]), $(name[4]), $(name[5]))
nothing
Expand All @@ -10,6 +10,23 @@ function construct_residuals(name, function_body, args)
function_body[i] = :($(name[1])[$i] = $(function_body[i]))
end

# add function body to function
f!.args[2].args[end] = Expr(:block, function_body...)
return f!
end

function construct_residuals(name, function_body)
f! = quote
($(name[1]), $(name[2]), $(name[3]), $(name[4]), $(name[5])) -> begin
nothing
end
end

# construct function body
for i in eachindex(function_body)
function_body[i] = :($(name[1])[$i] = $(function_body[i]))
end

# add function body to function
f!.args[2].args[end] = Expr(:block, function_body...)
return f!
Expand Down
13 changes: 8 additions & 5 deletions src/Macros.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function build_f!(endos, exos, params, args)
function build_f!(endos, exos, params, args, verbose=false)
endos = endos.variables
exos = exos.variables
params = params.variables
Expand Down Expand Up @@ -26,7 +26,11 @@ function build_f!(endos, exos, params, args)
end

# construct function for residuals of model variables
return MacroTools.striplines(:(Consistent.f! = $(construct_residuals(name, function_body, args))))
if verbose
return construct_residuals_for_print(name, function_body)
else
return construct_residuals(name, function_body)
end
end

"""
Expand Down Expand Up @@ -80,16 +84,15 @@ function model(;
end

if verbose
println(build_f!(endos, exos, parameters, eqs.exprs))
println(MacroTools.striplines(build_f!(endos, exos, parameters, eqs.exprs, true).args[2]))
end

eval(build_f!(endos, exos, parameters, eqs.exprs))
return Model(
endos,
exos,
parameters,
eqs,
deepcopy(Consistent.f!)
eval(build_f!(endos, exos, parameters, eqs.exprs))
)
end

Expand Down

0 comments on commit cb3831a

Please sign in to comment.