Skip to content

Commit

Permalink
Make sure the solution function is called right before return
Browse files Browse the repository at this point in the history
  • Loading branch information
kagalenko-m-b committed Jan 24, 2025
1 parent 8023041 commit 09a8bbb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name = "ProblemSet"
uuid = "806f51fd-16a3-4f55-bd44-f0d72f9cf926"
authors = ["kagalenko-m-b <[email protected]> and contributors"]
version = "0.7.9"
version = "0.8"

[deps]
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
MacroTools = "0.5.13"
MacroTools = "0.5"
julia = "1.9"

[extras]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

[![Build Status](https://github.com/kagalenko-m-b/ProblemSet.jl/workflows/CI/badge.svg)](https://github.com/kagalenko-m-b/ProblemSet.jl/actions)

The goal of this package is to facilitate the creation of problem assignments
for a group of students. It allows to randomize the assignments in two ways:
This package facilitates creation of assignments for a group of students. It allows
to randomize the assignments in two ways:

* [random selection](Random_selection.md) of problems from a set, and

Expand Down
2 changes: 1 addition & 1 deletion Random_selection.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A simplest set may be created by giving to the `@problemset` macro a list of questions
as a multiline string:
```julia
question_set = questions"""The first question.
question_set = questions"The first question.
The second question.
...
The tenth question."
Expand Down
24 changes: 13 additions & 11 deletions src/problem_compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ function process_body(mod, name, body)
allunique(sol_vars) || @warn("duplicate variables at the left-hand "
*"side of the tilde operator in @solution")
problemdef[:sol_vars] = unique(sol_vars)
#
# Solution function should be called at the last line before return,
# rather than at the line where the @solution macro is found
body_args = map(x->is_macro(x, :solution) ? :(:solution) : x, body_args)
cond_body = Expr(body.head, body_args...)
cond_vars = Symbol[]
Expand Down Expand Up @@ -138,16 +139,17 @@ function build_output(problemdef, linenumbernode)
idx_solution = findfirst(x-> x == :(:solution), cond_args)
# in place of @solution macro, insert into the problem's condition
# call to the solution function
if isempty(problemdef[:sol_vars])
sol_expr = Expr(:call, problemdef[:name], problemdef[:cond_vars]...)
if isnothing(idx_solution)
sol_expr = nothing
else
sol_expr = Expr(Symbol("="), Expr(:tuple, problemdef[:sol_vars]...),
Expr(:call, problemdef[:name], problemdef[:cond_vars]...))
end
if !isnothing(idx_solution)
cond_args[idx_solution] = sol_expr
cond_body.args = [linenumbernode; cond_body.args; return_expr]
if isempty(problemdef[:sol_vars])
sol_expr = Expr(:call, problemdef[:name], problemdef[:cond_vars]...)
else
sol_expr = Expr(Symbol("="), Expr(:tuple, problemdef[:sol_vars]...),
Expr(:call, problemdef[:name], problemdef[:cond_vars]...))
end
end
cond_body.args = [linenumbernode; cond_body.args; sol_expr; return_expr]
problemdef[:body] = cond_body
ex_cond_function = MacroTools.combinedef(problemdef)
#
Expand Down Expand Up @@ -207,10 +209,10 @@ end

function macro_body(expr, name, n_args=1)
is_macro(expr, name) || return nothing
args = filter(e -> !(e isa LineNumberNode), expr.args)
args = filter(e -> !((e isa LineNumberNode) || isnothing(e)), expr.args)
if length(args) != n_args+1
throw(ArgumentError("number of arguments of macro @$(string(name)) "*
"must be equal to $n_args, but is $(length(args))"))
"must be equal to $n_args, but is $(length(args) - 1)"))
end
args[end]
end
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ problem_set = :(@problemset test_problem_set begin
@test pr.args[1] == Base.remove_linenums!(:(function sub_add(; )
z = rand(7:9)
w = rand(1:5)
:solution
(zw_sub, zw_add) = sub_add(z, w)
return (;z, w, zw_sub, zw_add)
end)
Expand Down

2 comments on commit 09a8bbb

@kagalenko-m-b
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes: Reduced unneeded variability in generated functions

Breaking changes

  • This changes the way the problem solution function is placed within the main method. Unless you tried to do something unusual, this should not influence your problem sets.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/123651

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.0 -m "<description of version>" 09a8bbb41dbfad4e2513adc3e4e2edc08214c163
git push origin v0.8.0

Please sign in to comment.