Skip to content

genkuroki/MetaUtils.jl

Repository files navigation

MetaUtils - Utilities for metaprogramming in Julia

Dev Build Status

This package provides the utilities not found in InteractiveUtils and Meta modules. This is the renamed and enhanced version of the deprecated package InteractiveUtilsPlus.jl.

Installation

Add this package by REPL package manager:

julia> ]
pkg> add https://github.com/genkuroki/MetaUtils.jl

Or, add this package using Pkg.jl.

julia> using Pkg; Pkg.add(url="https://github.com/genkuroki/MetaUtils.jl")

Examples

For the detaild usage, see the Jupyter notebook MetaUtils.ipynb.

julia> using MetaUtils
julia> @show_sexpr 2x+1
(:call, :+, (:call, :*, 2, :x), 1)
julia> x = 10; (:call, :+, (:call, :*, 2, :x), 1) |> teval
21
julia> @show_tree 2x+1
Expr(:call)
├─ :+
├─ Expr(:call)
│  ├─ :*
│  ├─ 2
│  └─ :x
└─ 1
julia> show_tree(:(2x+1))
Expr(:call)
├─ :+
├─ Expr(:call)
│  ├─ :*
│  ├─ 2
│  └─ :x
└─ 1
julia> print_subtypes(AbstractRange)
AbstractRange
├─ LinRange
├─ OrdinalRange
│  ├─ AbstractUnitRange
│  │  ├─ Base.IdentityUnitRange
│  │  ├─ Base.OneTo
│  │  ├─ Base.Slice
│  │  └─ UnitRange
│  └─ StepRange
└─ StepRangeLen
julia> show_expr(:(f(x, g(y, z))))
Expr(:call, :f, :x, 
    Expr(:call, :g, :y, :z))
julia> @show_expr 2x+1
Expr(:call, :+, 
    Expr(:call, :*, 2, :x), 1)
julia> x = 10; Expr(:call, :+, 
    Expr(:call, :*, 2, :x), 1) |> eval
21
julia> show_expr(:(f(x, g(y, z))))
Expr(:call, :f, :x, 
    Expr(:call, :g, :y, :z))
julia> @show_texpr 2x+1
(:call, :+, 
    (:call, :*, 2, :x), 1)
julia> x = 10; (:call, :+, 
    (:call, :*, 2, :x), 1) |> teval
21
julia> (:call, :sin, (:call, :/, π, 6)) |> teval
0.49999999999999994
julia> @teval (:call, :sin, (:call, :/, π, 6))
0.49999999999999994
julia> (:sin, (:/, π, 6)) |> teval
0.49999999999999994
julia> @teval (:sin, (:/, π, 6))
0.49999999999999994
julia> MetaUtils.@t (:call, :sin, (:call, :/, π, 6))
:(sin/ 6))
 0.49999999999999994
julia> MetaUtils.@T (:call, :sin, (:call, :/, π, 6))
(:call, :sin, (:call, :/, π, 6))
 (:call, :sin, 
    (:call, :/, π, 6))
 :(sin/ 6))
 0.49999999999999994