Skip to content

Latest commit

 

History

History
514 lines (412 loc) · 10.6 KB

MetaUtils.md

File metadata and controls

514 lines (412 loc) · 10.6 KB
jupyter
jupytext kernelspec
cell_metadata_json formats text_representation
true
ipynb,md
extension format_name format_version jupytext_version
.md
markdown
1.3
1.11.2
display_name language name
Julia 1.7.0-DEV
julia
julia-1.7

MetaUtils

Table of Contents

VERSION
if isfile("Project.toml")
    using Pkg
    Pkg.activate(".")
    using Revise
end
using MetaUtils

Explanatory examples

@show_sexpr 2x+1
x = 10; (:call, :+, (:call, :*, 2, :x), 1) |> teval
@show_tree 2x+1
show_tree(:(2x + 1))
print_subtypes(AbstractRange)
show_expr(:(f(x, g(y, z))))
@show_expr 2x+1
x = 10; Expr(:call, :+, 
    Expr(:call, :*, 2, :x), 1) |> eval
show_texpr(:(f(x, g(y, z))))
@show_texpr 2x+1
x = 10; (:call, :+, 
    (:call, :*, 2, :x), 1) |> teval
texpr2expr((:call, :sin, (:call, :/, π, 6)))
(:call, :sin, (:call, :/, π, 6)) |> teval
@teval (:call, :sin, (:call, :/, π, 6))
MetaUtils.@t (:call, :sin, (:call, :/, π, 6))
MetaUtils.@T (:call, :sin, (:call, :/, π, 6))
(:sin, (:/, π, 6)) |> teval
@teval (:sin, (:/, π, 6))
MetaUtils.@t (:sin, (:/, π, 6))
MetaUtils.@T (:sin, (:/, π, 6))

Miscellaneous examples of @show_texpr, etc.

for loop

@show_texpr for k in 1:10
    x = k*(k+1) ÷ 2
    println("k(k+1)/2 = ", x)
end
@show_texpr for k in 1:10
    x = k*(k+1) ÷ 2
    println("k(k+1)/2 = ", x)
end true
@show_tree for k in 1:10
    x = k*(k+1) ÷ 2
    println("k(k+1)/2 = ", x)
end 2
@show_tree for k in 1:10
    x = k*(k+1) ÷ 2
    println("k(k+1)/2 = ", x)
end
@show_tree for k in 1:10
    x = k*(k+1) ÷ 2
    println("k(k+1)/2 = ", x)
end 10 true
Meta.@dump for k in 1:10
    x = k*(k+1) ÷ 2
    println("k(k+1)/2 = ", x)
end
@show_expr for k in 1:10
    x = k*(k+1) ÷ 2
    println("k(k+1)/2 = ", x)
end
@show_texpr for k in 1:10
    x = k*(k+1) ÷ 2
    println("k(k+1)/2 = ", x)
end

subtype trees

print_subtypes(AbstractRange)
print_subtypes(Number)
print_subtypes(AbstractVector)

function definition

@show_texpr function f(x::T) where T<:Number
    sin(x)
end
@show_tree function f(x::T) where T<:Number
    sin(x)
end
@show_expr function f(x::T) where T<:Number
    sin(x)
end
@show_texpr function f(x::T) where T<:Number
    sin(x)
end

macro and LineNumberNode

@show_tree @show float(π)
@show_sexpr @show float(π)
@teval (:macrocall, Symbol("@show"), :(#= In[34]:1 =#), (:call, :float, ))
@show_expr @show float(π)
Expr(:macrocall, Symbol("@show"), LineNumberNode(@__LINE__, @__FILE__), 
    Expr(:call, :float, )) |> show_expr
Expr(:macrocall, Symbol("@show"), LineNumberNode(@__LINE__, @__FILE__), 
    Expr(:call, :float, )) |> eval
@show_texpr @show float(π)
(:macrocall, Symbol("@show"), LineNumberNode(@__LINE__, @__FILE__), 
    (:call, :float, )) |> teval
@teval (:macrocall, Symbol("@show"), LineNumberNode(@__LINE__, @__FILE__),  
    (:call, :float, ))

QuoteNode

QuoteNode(:(sin(x)))
QuoteNode(:(sin(x))) |> Meta.show_sexpr
QuoteNode(:(sin(x))) |> show_expr
QuoteNode(:(sin(x))) |> show_texpr
QuoteNode(
    (:call, :sin, :x)) |> texpr2expr == QuoteNode(:(sin(x)))
@teval QuoteNode(
    (:call, :sin, :x))

Evaluation of Lisp-like tuple expressions

If you want more Lisp-like examamples, see LispLikeEval.ipynb.

using MetaUtils: @t, @T
# Define and run a function f(x) = sin(x)

@t (:(=), :(f(x)), (:sin, :x))
println()
@t (:f, (:/, π, 6))
# Define and run a function f(x) = sin(x)

@T (:(=), :(f(x)), (:sin, :x))
println()
@T (:f, (:/, π, 6))
# Define and run a function g(x) = sin(x)

@t (:block,
    (:function, :(g(x)), (:sin, :x)),
    (:call, :g, (:/, π, 6)))
# Define and run a function g(x) = sin(x)

@T (:block,
    (:function, :(g(x)), (:sin, :x)),
    (:call, :g, (:/, π, 6)))
# Calculation of pi by the Monte Carlo method

@t (:block, 
    (:function, :(pi_mc(N)), 
        (:block, 
            (:(=), :c, 0), 
            (:for, (:(=), :i, (:(:), 1, :N)), 
                (:block, 
                    (:+=, :c, 
                        (:call, :ifelse, 
                            (:, (:+, (:^, (:rand,), 2), (:^, (:rand,), 2)), 1), 
                            1, 0)))), 
            (:/, (:*, 4, :c), :N))), 
    (:call, :pi_mc, (:^, 10, 8)))
# quote

@t (:quote, (:sin, :x))
# tuple

@t (:tuple, 1, 2, 3)

Plot example

begin
    using Plots
    n = 20
    x = range(-π, π; length=20)
    noise = 0.3randn(n)
    y = sin.(x) + noise
    X = x .^ (0:3)'
    b = X\y
    f(x) = evalpoly(x, b)
    xs = range(-π, π; length=400)
    plot(; legend=:topleft)
    scatter!(x, y; label="sample")
    plot!(xs, sin.(xs); label="sin(x)", color=:blue, ls=:dash)
    plot!(xs, f.(xs); label="degree-3 polynomial", color=:red, lw=2)
end
@show_texpr begin
    using Plots
    n = 20
    x = range(-π, π; length=20)
    noise = 0.3randn(n)
    y = sin.(x) + noise
    X = x .^ (0:3)'
    b = X\y
    f(x) = evalpoly(x, b)
    xs = range(-π, π; length=400)
    plot(; legend=:topleft)
    scatter!(x, y; label="sample")
    plot!(xs, sin.(xs); label="sin(x)", color=:blue, ls=:dash)
    plot!(xs, f.(xs); label="degree-3 polynomial", color=:red, lw=2)
end
@teval (:block, 
    (:using, (:., :Plots)), 
    (:(=), :n, 20), 
    (:(=), :x, (:range, (:parameters, (:kw, :length, 20)), (:-, ), )), 
    (:(=), :noise, (:*, 0.3, (:randn, :n))), 
    (:(=), :y, (:+, (:., :sin, (:tuple, :x)), :noise)), 
    (:(=), :X, (:call, :.^, :x, (Symbol("'"), (:call, :(:), 0, 3)))),  
    (:(=), :b, (:\, :X, :y)), 
    (:(=), (:call, :f, :x), (:block, (:call, :evalpoly, :x, :b))),
    (:(=), :xs, (:range, (:parameters, (:kw, :length, 400)), (:-, ), )), 
    (:plot, (:parameters, (:kw, :legend, QuoteNode(:topleft)))), 
    (:scatter!, (:parameters, (:kw, :label, "sample")), :x, :y), 
    (:plot!, (:parameters, 
            (:kw, :label, "sin(x)"), 
            (:kw, :color, QuoteNode(:blue)), 
            (:kw, :ls, QuoteNode(:dash))), 
        :xs, (:., :sin, (:tuple, :xs))), 
    (:plot!, (:parameters, 
            (:kw, :label, "degree-3 polynomial"), 
            (:kw, :color, QuoteNode(:red)), 
            (:kw, :lw, 2)), 
        :xs, (:., :f, (:tuple, :xs))))
(:block, 
    (:using, (:., :Plots)), 
    (:(=), :n, 20), 
    (:(=), :x, (:range, (:parameters, (:kw, :length, 20)), (:-, ), )), 
    (:(=), :noise, (:*, 0.3, (:randn, :n))), 
    (:(=), :y, (:+, (:., :sin, (:tuple, :x)), :noise)), 
    (:(=), :X, (:call, :.^, :x, (Symbol("'"), (:call, :(:), 0, 3)))),  
    (:(=), :b, (:\, :X, :y)), 
    (:(=), (:call, :f, :x), (:block, (:call, :evalpoly, :x, :b))),
    (:(=), :xs, (:range, (:parameters, (:kw, :length, 400)), (:-, ), )), 
    (:plot, (:parameters, (:kw, :legend, QuoteNode(:topleft)))), 
    (:scatter!, (:parameters, (:kw, :label, "sample")), :x, :y), 
    (:plot!, (:parameters, 
            (:kw, :label, "sin(x)"), 
            (:kw, :color, QuoteNode(:blue)), 
            (:kw, :ls, QuoteNode(:dash))), 
        :xs, (:., :sin, (:tuple, :xs))), 
    (:plot!, (:parameters, 
            (:kw, :label, "degree-3 polynomial"), 
            (:kw, :color, QuoteNode(:red)), 
            (:kw, :lw, 2)), 
        :xs, (:., :f, (:tuple, :xs)))) |> texpr2expr |> 
x -> display("text/markdown", "```julia\n$x\n```")

Documents

@doc MetaUtils
@doc @show_sexpr
@doc @show_tree
@doc show_tree
@doc print_subtypes
@doc show_expr
@doc @show_expr
@doc show_texpr
@doc @show_texpr
@doc teval
@doc @teval
@doc MetaUtils.@t
@doc MetaUtils.@T