VERSION """ `@lattice_states(lattice::Expr, loop_level::Int)` Prints all possible `lattice_state` vectors`of a given `lattice` up to `loop_level` nesting. ## Parameters * `lattice`: A `Vector{Int}` literal expression, ie: `[1, -1]`. * `loop_level`: An `Int`, ie: `4`. ## Usage ``` julia> @lattice_states [-1, 1] 4 [-1,-1,-1,-1] [-1,-1,-1,1] [-1,-1,1,-1] [-1,-1,1,1] [-1,1,-1,-1] [-1,1,-1,1] [-1,1,1,-1] [-1,1,1,1] [1,-1,-1,-1] [1,-1,-1,1] [1,-1,1,-1] [1,-1,1,1] [1,1,-1,-1] [1,1,-1,1] [1,1,1,-1] [1,1,1,1] ``` ## Macro expansion ``` julia> macroexpand(:(@lattice_states [-1, 1] 4)) :(for #3#state = [-1,1] # none, line 71: ([0,0,0,0])[1] = #3#state # none, line 72: for #3#state = [-1,1] # none, line 71: ([0,0,0,0])[2] = #3#state # none, line 72: for #3#state = [-1,1] # none, line 71: ([0,0,0,0])[3] = #3#state # none, line 72: for #3#state = [-1,1] # none, line 71: ([0,0,0,0])[4] = #3#state # none, line 72: println([0,0,0,0]) end end end end) ``` ## Misusage ``` julia> @lattice_states [-1.0, 1.0] 4 ERROR: AssertionError: `lattice` must be a Vector{Int} literal expression, ie [1, -1], got: [-1.0,1.0]. julia> @lattice_states [-1, 1] 4.0 ERROR: MethodError: `@lattice_states` has no method matching @lattice_states(::Expr, ::Float64) julia> @lattice_states [-1, 1] -4 ERROR: AssertionError: loop_level must be an Int greater than 0, got: -1. ``` """ macro lattice_states(lattice::Expr, loop_level::Int) @assert( lattice.head == :vect && all(x -> issubtype(typeof(x), Int), lattice.args), "`lattice` must be a Vector{Int} literal expression, ie [1, -1], got: $lattice." ) @assert loop_level > 0 "loop_level must be an Int greater than 0, got: $loop_level." lattice_state = zeros(Int, loop_level) inner = :(println($lattice_state)) outer = inner for i = reverse(1:loop_level) outer = :( for state in $lattice $lattice_state[$i] = state $outer end ) end return outer end @lattice_states [-1, 1] 2 @lattice_states [-1, 1] 4 macroexpand(:(@lattice_states [-1, 1] 2)) macroexpand(:(@lattice_states [-1, 1] 4)) macroexpand(:(@lattice_states [-1, 1] 8)) @lattice_states [-1.0, 1.0] 2 @lattice_states [-1, 1] 2.0 @lattice_states [-1, 1] -1