Skip to content

Commit

Permalink
revert the macro to the simpler form as it had too many difficult cor…
Browse files Browse the repository at this point in the history
…ner cases
  • Loading branch information
fkastner committed Mar 11, 2020
1 parent f3a4d11 commit 8d4be40
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ There is also a macro to simplify working with nice numbers:
julia> n = norm([4,12,3] * 2)
18.38477631085024

julia> m = @nice norm([4,12,3] * 2)
julia> @nice m = norm([4,12,3] * 2)
Nice number:
0//1 + 13//1 2

Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ There is also a macro to simplify working with nice numbers:
julia> n = norm([4,12,3] * √2)
18.38477631085024
julia> m = @nice norm([4,12,3] * √2)
julia> @nice m = norm([4,12,3] * √2)
Nice number:
0//1 + 13//1 ⋅ √2
Expand Down
17 changes: 4 additions & 13 deletions src/NiceNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,29 +134,20 @@ If `x` is an expression it replaces all occuring numbers by `NiceNumber`s.
If `x` is a number it turns it into a `NiceNumber`.
If `x` is a symbol defined in `mod` as a `Number` or `AbstractArray{<:Number}` then it converts it into a `NiceNumber` or `AbstractArray{NiceNumber}`. This means the macro should only be used on the rhs of assignments.
Otherwise it does nothing.
"""
function nice end
nice(x, mod=@__MODULE__) = x
nice(n::Number, mod=@__MODULE__) = NiceNumber(n)
nice(ex::Expr, mod=@__MODULE__) = Expr(ex.head, map(a->nice(a,mod), ex.args)...)
function nice(s::Symbol, mod=@__MODULE__)
!isdefined(mod,s) && return s
type = typeof(mod.eval(s))
type <: Number && return Expr(:call,:NiceNumber,s)
type <: AbstractArray{<:Number} && return Expr(:.,NiceNumber,Expr(:tuple,s))
return s
end
nice(x) = x
nice(n::Number) = NiceNumber(n)
nice(ex::Expr) = Expr(ex.head, map(nice, ex.args)...)

"""
@nice
Return equivalent expression with all numbers converted to `NiceNumber`s.
"""
macro nice(code)
return esc(nice(code, __module__))
return esc(nice(code))
end

end # module

0 comments on commit 8d4be40

Please sign in to comment.