I’m looking for a way to check whether a vector contains a sub-vector matching a certain conditional pattern. I wrote a quick and dirty macro doing this, here’s an example to illustrate what I mean:
julia> f=@match i, j, k (i==k) && (j>= k+1)
#194#f (generic function with 1 method)
julia> f([1,3,1,4]) #the sub-vector [1,3,1] matches the condition
true
julia> f([1,0,1,4])
false
Basically my question is: is there an existing package doing this ? I had a look at Match.jl, Metatheory.jl and MLStyle.jl and if doesn’t seem they can. Below is the code for my macro. I tried manipulating the AST directly but that was kind of a pain, so I end up converting the expression into a string, replacing and parsing back, which I guess is not very elegant. Do you see a better way to do this ?
function subst(s, w::String)
o=w
for i in 1:length(s)
o=replace(o, string(s[i])=>"v[i+$(i-1)]")
end
return o
end
macro match(seq,cond)
p=length(seq.args)
str=string(cond)
str=subst(seq.args,str)
c=Meta.parse(str)
quote
v-> begin
for i in 1:length(v)-$p+1
if $c
return true
end
end
return false
end
end
end