From 80a7132e26ee66304b395b2420f7da888bb6f430 Mon Sep 17 00:00:00 2001 From: spirosbax Date: Fri, 6 Mar 2020 22:41:56 +0200 Subject: [PATCH 1/2] add broadcast functionality plus tests --- src/Hose.jl | 27 +++++++++++++++++++-------- test/runtests.jl | 3 ++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Hose.jl b/src/Hose.jl index 1146231..9d1704b 100644 --- a/src/Hose.jl +++ b/src/Hose.jl @@ -7,24 +7,31 @@ export @hose const PLACEHOLDER = :_ -rewrite_apply(ff::Symbol, target) = :($ff($target)) # Function/macro application +function rewrite_apply(ff::Symbol, target, b::Bool = false) + if b + :(broadcast($ff,$target)) + else + :($ff($target)) # Function/macro application + end +end -function rewrite_apply(ff::Expr, target) +function rewrite_apply(ff::Expr, target, b::Bool = false) if ff.head == :call insert!(ff.args, 2, target) ff elseif ff.head == :macrocall insert!(ff.args, 3, target) ff + elseif b + :(broadcast($ff,$target)) else :($ff($target)) end end -rewrite(ff::Symbol, target) = ifelse(ff == PLACEHOLDER, target, - rewrite_apply(ff, target)) +rewrite(ff::Symbol, target, b::Bool = false) = ifelse(ff == PLACEHOLDER, target, rewrite_apply(ff, target, b)) -function rewrite(ff::Expr, target) +function rewrite(ff::Expr, target, b::Bool = false) replace(arg::Any) = arg # For most things should be an identity. replace(arg::Symbol) = ifelse(arg == PLACEHOLDER, target, arg) # Placeholder symbol should get replaced. function replace(arg::Expr) @@ -38,16 +45,20 @@ function rewrite(ff::Expr, target) ff.args = rep_args # Placeholder subsitution ff else # No subsitution was done (no placeholder symbol found) - rewrite_apply(ff, target) # Apply to a function/macro that is being returned by ff (ff could be a function call or something more complex). + rewrite_apply(ff, target, b) # Apply to a function/macro that is being returned by ff (ff could be a function call or something more complex). end end funnel(ee::Any) = ee # Identity for first (left most) input. function funnel(ee::Expr) - if (ee.args[1] == :|>) # If ee is a call to |> + if (ee.args[1] == :|> || ee.args[1] == :.|>) # If ee is a call to |> target = funnel(ee.args[2]) # Process left hand side - rewrite(ee.args[3], target) # Rewrite |> right hand side using left hand side + if (ee.args[1] == :.|>) + rewrite(ee.args[3], target, true) # Rewrite |> right hand side using left hand side + else + rewrite(ee.args[3], target, false) # Rewrite |> right hand side using left hand side + end else ee # Not in a piping situtation end diff --git a/test/runtests.jl b/test/runtests.jl index 6bdb91c..a9a2cc8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,7 +19,8 @@ macroexpand(q) = macroexpand(Main, q) @test macroexpand(:(@hose "foo"|>a)) == :(a("foo")) #Works with literal (string) @test macroexpand( :(@hose a|>bb[2])) == :((bb[2])(a)) #Should work with RHS that is a array reference - +# Test Problem +@test macroexpand( :(@th a|>bb[2])) == :((bb[2])(a)) #Should work with RHS that is a array reference #Marked locations @test macroexpand( :(@hose a |> _)) == :(a) #Identity works From 6eed2fc82ca631015cc77883a44a147e559a079d Mon Sep 17 00:00:00 2001 From: spirosbax Date: Fri, 6 Mar 2020 22:48:10 +0200 Subject: [PATCH 2/2] fix tests --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index a9a2cc8..45f1d00 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -20,7 +20,7 @@ macroexpand(q) = macroexpand(Main, q) @test macroexpand( :(@hose a|>bb[2])) == :((bb[2])(a)) #Should work with RHS that is a array reference # Test Problem -@test macroexpand( :(@th a|>bb[2])) == :((bb[2])(a)) #Should work with RHS that is a array reference +@test macroexpand( :(@hose a|>bb[2])) == :((bb[2])(a)) #Should work with RHS that is a array reference #Marked locations @test macroexpand( :(@hose a |> _)) == :(a) #Identity works