我将这个宏封装到module 中
module Chain
macro thread_as(nums_expr,as,exprs...)
fn(from,as,to) = Expr(:call,replace(to.args,as=>from)...)
iter = function iter(exprs,res)
if length(exprs) == 1
fn(res,as,first(exprs))
else
iter(Iterators.drop(exprs,1),fn(res,as,first(exprs)))
end
end
iter(exprs,nums_expr)
end
end
并且导入我的fib模块
module Fib
mutable struct Fibs
value::Int
rest::Fibs
Fibs(v) = new(v)
Fibs(v, r) = new(v, r)
end
fib = Fibs(0, Fibs(1))
function Base.iterate(fibs::Fibs, (a,b)=(fibs, fibs.rest))
value = a.value + b.value
if !isdefined(b, :rest)
b.rest = Fibs(value)
end
return value, (b, b.rest)
end
我需要调用
@thread_as Iterators.take(fib,40) _ collect(_) filter(x->x<500,_)
这是没有用模块包装@thread_as
展开的代码
julia> @macroexpand1 @thread_as Iterators.take(fib,40) _ collect(_) filter(x->x<500,_)
:(Main.filter((var"#7#x"->begin
#= REPL[6]:1 =#
var"#7#x" < 500
end), Main.collect((Main.Iterators).take(Main.fib, 40))))
这是使用模块包装@thread_as
扔出的错误
julia> @thread_as Iterators.take(fib,40) _ collect(_) filter(x->x<500,_)
ERROR: UndefVarError: fib not defined
Stacktrace:
[1] top-level scope at REPL[6]:1
这是展开的代码
julia> @macroexpand1 @thread_as Iterators.take(fib,40) _ collect(_) filter(x->x<500,_)
:(Main.Chain.filter((var"#8#x"->begin
#= REPL[7]:1 =#
var"#8#x" < 500
end), Main.Chain.collect((Main.Chain.Iterators).take(Main.Chain.fib, 40))))
这里宏代码展开出现了错误,这应该怎么改