On Wednesday, June 18, 2014 06:06:24 AM paul analyst wrote:
> Thanks, but I'm an analyst market, not a programmer;/ Still, I really like
> Julia. Historically, only SPSS. I can so deeply programmed? Any tips?
Yes, with Julia it's easy to reach in "deep." First you might want to look at
existing methods for setdiff:
julia> methods(setdiff)
# 3 methods for generic function "setdiff":
setdiff(a::IntSet,b::IntSet) at intset.jl:90
setdiff(a::Set{T},b::Set{T}) at set.jl:73
setdiff(a,b) at array.jl:1352
From this, personally I'd guess the implementation in array.jl will be closest
to what you want. So open up an editor and look at the implementation in
base/array.jl (I'll add a few comments to help you):
# from array.jl, line 1352:
function setdiff(a, b)
# The first few lines initialize an empty output Vector
# with the correct element type,
# and convert b to a Set so that it's fast to test whether
# an element of a can be found in b.
args_type = promote_type(eltype(a), eltype(b))
bset = Set(b)
ret = Array(args_type,0)
# seen is to make sure we return just the unique values in a
seen = Set()
for a_elem in a # loop over all the elements in a
# test that we haven't seen a_elem before,
# and that it's not present in b
if !in(a_elem, seen) && !in(a_elem, bset)
push!(ret, a_elem) # keep the element
push!(seen, a_elem) # mark it as seen
end
end
ret # return the result
end
That's it!
For implementing your own version, start like this:
import Base.setdiff
function setdiff(a::Ranges, b::Ranges)
# code goes here
end
This means your version will be called whenever both inputs are Range-type
objects.
For the actual implementation, here's another hint: you probably noticed that
most of the "magic" was taken care of by the "in" function. Why not check to
see whether there are any methods already defined for Range objects? If so, you
can expect that they might be more efficient that for Sets. Also, you probably
don't have to worry about "seen" because the elements of the range are
guaranteed to be unique (if step(a) != 0). So, you could almost cut-and-paste
that implementation for arrays, but delete all the parts that deal with Sets.
Once you've developed the code, try posting it here for comments. Eventually
you might want to learn git, but let's take one step at a time :-).
--Tim