Skip to content

Commit

Permalink
Merge pull request #49 from rafaqz/macro_hygene
Browse files Browse the repository at this point in the history
Macro hygene
  • Loading branch information
ChrisRackauckas authored Mar 1, 2019
2 parents b5b1a40 + 9c12f45 commit 1cb485a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
7 changes: 7 additions & 0 deletions src/larray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,17 @@ For example:
b = @LArray [1,2,3] (:a,:b,:c)
"""
macro LArray(vals,syms)
vals = esc(vals)
syms = esc(syms)
return quote
LArray{$syms}($vals)
end
end

macro LArray(type,size,syms)
type = esc(type)
size = esc(size)
syms = esc(syms)
return quote
LArray{$syms}(Array{$type}(undef,$size...))
end
Expand All @@ -137,6 +142,8 @@ For example:
b = @LVector [1,2,3] (:a,:b,:c)
"""
macro LVector(type,syms)
type = esc(type)
syms = esc(syms)
return quote
LArray{$syms}(Vector{$type}(undef,length($syms)))
end
Expand Down
9 changes: 7 additions & 2 deletions src/lsliced.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,18 @@ For example:
b = @LSliced [1 2; 3 4; 5 6] (:a,:b,:c), (:x,:y)
"""
macro LSliced(vals,syms)
vals = esc(vals)
syms = esc(syms)
return quote
LArray{Tuple{$syms...,}}($vals)
LArray{Tuple{$syms...,}}($vals)
end
end

macro LSliced(type,size,syms)
type = esc(type)
size = esc(size)
syms = esc(syms)
return quote
LArray{Tuple{$syms...,}}(Array{$type}(undef,$size...))
LArray{Tuple{$syms...,}}(Array{$type}(undef,$size...))
end
end
11 changes: 7 additions & 4 deletions src/slarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,18 @@ x.c == x[3]
"""
macro SLVector(syms)
n = syms isa Expr ? length(syms.args) : length(syms)
syms = esc(syms)
quote
SLArray{Tuple{$n},$syms}
n = length($syms)
SLArray{Tuple{n},$syms}
end
end

macro SLVector(T,syms)
n = syms isa Expr ? length(syms.args) : length(syms)
T = esc(T)
syms = esc(syms)
quote
SLArray{Tuple{$n},$T,1,$n,$syms}
n = length($syms)
SLArray{Tuple{n},$T,1,n,$syms}
end
end
9 changes: 6 additions & 3 deletions src/slsliced.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ x.d.y == x[4,2]
"""
macro SLSliced(dims,syms)
dims isa Expr && (dims = dims.args)
dims = esc(dims)
syms = esc(syms)
quote
SLArray{Tuple{$dims...,},Tuple{$syms...,}}
end
end

macro SLSliced(T,dims,syms)
dims isa Expr && (dims = dims.args)
T = esc(T)
dims = esc(dims)
syms = esc(syms)
quote
SLArray{Tuple{$dims...},$T,$(length(dims)),$(prod(dims)),Tuple{$syms...}}
SLArray{Tuple{$dims...},$T,length($dims),prod($dims),Tuple{$syms...}}
end
end
13 changes: 10 additions & 3 deletions test/larrays.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using LabelledArrays, Test, InteractiveUtils

@testset "Basic interface" begin
x = @LArray [1.0,2.0,3.0] (:a,:b,:c)
vals = [1.0,2.0,3.0]
syms = (:a,:b,:c)
x = @LArray vals syms
y = @LVector Float64 (:a,:b,:c)
y .= [1,2,3.]
@test x == y
Expand All @@ -27,7 +29,9 @@ using LabelledArrays, Test, InteractiveUtils
@code_warntype g(x)
@inferred g(x)

x = @LArray [1,2,3] (:a,:b,:c)
vals = [1,2,3]
syms = (:a,:b,:c)
x = @LArray vals syms
@test x .* x isa LArray
@test x .+ 1 isa LArray
@test x .+ 1. isa LArray
Expand All @@ -37,7 +41,10 @@ using LabelledArrays, Test, InteractiveUtils
@test z isa LArray && eltype(z) === Float64
@test eltype(x .+ 1.) === Float64

z = @LArray Float64 (2,2) (:a,:b,:c,:d)
type = Float64
dims = (2,2)
syms = (:a,:b,:c,:d)
z = @LArray type dims syms
w = rand(2,2)
z .= w

Expand Down
4 changes: 3 additions & 1 deletion test/lsliced.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ using LabelledArrays, Test, InteractiveUtils
@test z[:a,:d] == w[1,2]
@test z[:b,:d] == w[2,2]

x = @LSliced [1 2; 3 4; 5 6] (:a,:b,:c), (:x, :y)
vals = [1 2; 3 4; 5 6]
syms = (:a,:b,:c), (:x, :y)
x = @LSliced vals syms
x.a.x = 33
x.b.y = 99
x[:a,:y] = 44
Expand Down

0 comments on commit 1cb485a

Please sign in to comment.