Skip to content

Use $ to eagerly evaluate expressions in @~ #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/lazymacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ Broadcast.materialize(x::LazyCast) = x.value

is_call(ex) = isexpr(ex, :call) && !is_dotcall(ex)

is_dotcall(ex) =
(isexpr(ex, :.) && isexpr(ex.args[2], :tuple)) ||
(isexpr(ex, :call) && ex.args[1] isa Symbol && startswith(String(ex.args[1]), "."))
# e.g., `f.(x, y, z)` or `x .+ y .+ z`
is_dotcall(ex) = is_dotcall_fn(ex) || is_dotcall_op(ex)

is_dotcall_fn(ex) = (isexpr(ex, :.) && isexpr(ex.args[2], :tuple))
# e.g., `f.(x, y, z)`

function is_dotcall_op(ex)
isexpr(ex, :call) && !isempty(ex.args) || return false
op = ex.args[1]
return op isa Symbol && Base.isoperator(op) && startswith(string(op), ".")
end
# e.g., `x .+ y .+ z`

lazy_expr(x) = x
function lazy_expr(ex::Expr)
Expand All @@ -40,12 +47,12 @@ end
bc_expr_impl(x) = x
function bc_expr_impl(ex::Expr)
# walk down chain of dot calls
if ex.head == :. && ex.args[2].head === :tuple
if is_dotcall_fn(ex)
@assert length(ex.args) == 2 # argument is always expressed as a tuple
f = ex.args[1] # function name
args = ex.args[2].args
return Expr(ex.head, lazy_expr(f), Expr(:tuple, bc_expr_impl.(args)...))
elseif ex.head == :call && startswith(String(ex.args[1]), ".")
elseif is_dotcall_op(ex)
f = ex.args[1] # function name (e.g., `.+`)
args = ex.args[2:end]
return Expr(ex.head, lazy_expr(f), bc_expr_impl.(args)...)
Expand All @@ -64,6 +71,10 @@ app_expr_impl(x) = x
function app_expr_impl(ex::Expr)
# walk down chain of calls and lazy-ify them
if is_call(ex)
if isexpr(ex.args[1], :$)
# eagerly evaluate the call
return Expr(:call, ex.args[1].args[1], ex.args[2:end]...)
end
return :($applied($(app_expr_impl.(ex.args)...)))
else
return lazy_expr(ex)
Expand Down
2 changes: 1 addition & 1 deletion test/lazymultests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ LinearAlgebra.factorize(A::MyLazyArray) = factorize(A.data)
@test apply(*,A,x) isa ApplyVector
@test apply(*,A,Array(x)) isa ApplyVector
@test apply(*,Array(A),x) isa ApplyVector
@test apply(*,A,x) ≈ apply(*,Array(A),x) ≈ apply(*,A,Array(x)) ≈ Array(A)*Array(x)
@test apply(*,A,x) ≈ apply(*,Array(A),x) ≈ apply(*,A,Array(x)) ≈ Array(A)*Array(x)

@test apply(*,A,B) isa ApplyMatrix
@test apply(*,A,Array(B)) isa ApplyMatrix
Expand Down
19 changes: 19 additions & 0 deletions test/macrotests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,23 @@ end
@test bc.args[1].args isa Tuple{Applied, Int}
end

@testset "@~ and \$" begin
A = ones(1, 1)
x = [1]

# Use `$` to evaluate a sub-expression eagerly
bc = @~ A .+ $Ref(x)
@test bc isa Broadcasted
@test bc.args[1] === A
@test bc.args[2] isa Ref # not Applied
@test bc.args[2][] === x

# Use `$$` when combined with `@.`
bc = @~ @. A + $$Ref(x)
@test bc isa Broadcasted
@test bc.args[1] === A
@test bc.args[2] isa Ref # not Applied
@test bc.args[2][] === x
end

end # module