-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6841736
commit be35bf2
Showing
4 changed files
with
51 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# TODO | ||
|
||
* Define nice show methods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ function pipe(expr) | |
end | ||
|
||
macro pipe(expr) | ||
esc(pipe(expr)) | ||
pipe(expr) | ||
end | ||
|
||
@testset "Pipe" begin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using ExprManipulation | ||
using Base.Meta: show_sexpr | ||
|
||
match_assign = MExpr(:(=), Capture(:lexpr), Capture(:rexpr)) | ||
match_tuple = MExpr(:tuple, Slurp(x -> x isa Array{Symbol} ,:keys)) | ||
|
||
function create_expr(lexpr, rexpr) | ||
@assert match_tuple == lexpr "Unsupported left hand expresion $lexpr" | ||
tmp = gensym() | ||
expr = Expr(:block, :($tmp = $rexpr)) | ||
args = [:($key = unpack($tmp, Val{$(Expr(:quote, key))}())) for key in lexpr.args ] | ||
append!(expr.args, args) | ||
expr | ||
end | ||
|
||
create_expr(lexpr::Symbol, rexpr) = :($lexpr = unpack($rexpr, Val{$(Expr(:quote, lexpr))}())) | ||
|
||
unpack(x, ::Val{k}) where {k} = getproperty(x, k) | ||
unpack(x::AbstractDict{Symbol}, ::Val{k}) where {k} = x[k] | ||
unpack(x::AbstractDict{<:AbstractString}, ::Val{k}) where {k} = x[string(k)] | ||
|
||
function unpack(expr) | ||
matches = match(match_assign, expr) | ||
!isnothing(matches) ? create_expr(matches.lexpr, matches.rexpr) : error("Unsuppored expression $expr") | ||
end | ||
|
||
macro unpack(input_expr) | ||
esc(unpack(input_expr)) | ||
end | ||
|
||
struct Data | ||
a | ||
b | ||
c | ||
d | ||
e | ||
f | ||
end | ||
|
||
data = Data(1, 2, 3 ,4, 5, 6) | ||
dict = Dict(:y=>2, "z"=>3) | ||
|
||
@unpack a, b, f = data | ||
@unpack d = data | ||
@unpack y, z = dict |