unionSet or intersectSet of two NSMutableSets?

asobjc

#1

I’m failing the IQ test on unions and intersections of NSSet or NSMutableSet objects – can anyone show me how to do this ?

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions


on run
    set ca to current application
    set s1 to setFromList(ft(1, 10))
    set s2 to setFromList(ft(7, 15))
    
    log elems(s1)
    log elems(s2)
    
    -- union or intersection of s1 and s2 ?
    
    
    -- (clearing the pointers, so that the script can be saved)
    set {s1, s2} to {missing value, missing value}
end run


-- elems:: Set a -> [a]
on elems(s)
    (s's allObjects()) as list
end elems



-- NB All names of NSMutableSets should be set to *missing value*
-- before the script exits.
-- ( scpt files can not be saved if they contain ObjC pointer values )
-- setFromList :: Ord a => [a] -> Set a
on setFromList(xs)
    set ca to current application
    ca's NSMutableSet's ¬
        setWithArray:(ca's NSArray's arrayWithArray:(xs))
end setFromList


-- ft :: Int -> Int -> [Int]
on ft(m, n)
    if m ≤ n then
        set lst to {}
        repeat with i from m to n
            set end of lst to i
        end repeat
        return lst
    else
        return {}
    end if
end ft

#2

Ah … didn’t spot the void - so unionSet and intersectionSet are mutations:

s1's unionSet:(s2)  -- s1 expanded to the union

(I had been looking for a derivation (without mutation) of a new set object)


(Shane Stanley) #3

As soon as you are dealing with a class with mutable in its name, you’re almost always dealing in instance methods that mutate.