• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Union of sets (lists
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Union of sets (lists


  • Subject: Re: Union of sets (lists
  • From: Kai <email@hidden>
  • Date: Sun, 12 Jan 2003 15:18:32 +0000

on Sat, 11 Jan 2003 16:49:51 -0700, Michelle Steiner <email@hidden>
wrote:

> set SetA to {"a", "b"} -- elements for set A
> set SetB to {"c", "d", "e"} -- elements for set B and so on....
> set foo to union(SetA, SetB)
>
> to union(a, b)
> if (class of a is not list) or (class of b is not list) then
> display dialog "Only lists can be used." buttons {"Cancel"}
> default button 1
> end if
>
> if (count (a)) is greater than (count (b)) then
> copy a to x
> copy b to y
> else
> copy b to x
> copy a to y
> end if
> set FinalResult to y
> repeat with i in x
> if (i is not in y) and (i is not in FinalResult) then
> copy i to end of FinalResult
> end if
> end repeat
> end union

Although the main query has now been dealt with, Michelle, I'm still kinda
curious about the purpose of the switcheroo in this routine - that is:

--------------------------------------------------
if (count (a)) is greater than (count (b)) then...
--------------------------------------------------

This may have something to do with the original situation, which the example
wouldn't necessarily indicate. However, if it's not critical which list is
added to which, it may be worth considering reversing the switch - so that
the repeat loop is based on the shorter list (thus speeding up the process)
- perhaps something like this:

==================================================
set SetA to {"a", "b"}
set SetB to {"c", "d", "e", "a", "f"}
set foo to addNewItems from SetB to SetA

to addNewItems from b to a
if a's class is not list or b's class is not list then [NO BREAK]
display dialog "Only lists can be used." buttons {"Cancel"} default button 1
if (count a) is greater than (count b) then set {a, b} to {b, a}
repeat with i in a
tell i's contents to if it is not in b then copy it to b's end
end repeat
b
end addNewItems
==================================================

The time savings are roughly proportional to the difference between the
lists' lengths.

This still assumes that the original lists contain no duplicates within
themselves - otherwise Deivy's point would also need to be considered:

==================================================
to addNewItems from b to a
if a's class is not list or b's class is not list then [NO BREAK]
display dialog "Only lists can be used." buttons {"Cancel"} default button 1
set {c, d} to {a & b, {}}
repeat with i in c
tell i's contents to if it is not in d then copy it to d's end
end repeat
d
end addNewItems
==================================================

(Clearly, since it loops through a longer list, this last version would also
be slightly slower...)

--
Kai
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Union of sets (lists
      • From: Michelle Steiner <email@hidden>
  • Prev by Date: Whoops! Correction -- Re: Creating Text Notes From Safari Script
  • Next by Date: Opening a local HTML file in the default browser
  • Previous by thread: Fwd: Union of sets (lists
  • Next by thread: Re: Union of sets (lists
  • Index(es):
    • Date
    • Thread