How to resolve the algorithm Symmetric difference step by step in the AppleScript programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Symmetric difference step by step in the AppleScript programming language
Table of Contents
Problem Statement
Given two sets A and B, compute
( A ∖ B ) ∪ ( B ∖ A ) .
{\displaystyle (A\setminus B)\cup (B\setminus A).}
That is, enumerate the items that are in A or B but not both. This set is called the symmetric difference of A and B. In other words:
( A ∪ B ) ∖ ( A ∩ B )
{\displaystyle (A\cup B)\setminus (A\cap B)}
(the set of items that are in at least one of A or B minus the set of items that are in both A and B). Optionally, give the individual differences (
A ∖ B
{\displaystyle A\setminus B}
and
B ∖ A
{\displaystyle B\setminus A}
) as well.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Symmetric difference step by step in the AppleScript programming language
Source code in the applescript programming language
-- SYMMETRIC DIFFERENCE -------------------------------------------
-- symmetricDifference :: [a] -> [a] -> [a]
on symmetricDifference(xs, ys)
union(difference(xs, ys), difference(ys, xs))
end symmetricDifference
-- TEST -----------------------------------------------------------
on run
set a to ["John", "Serena", "Bob", "Mary", "Serena"]
set b to ["Jim", "Mary", "John", "Jim", "Bob"]
symmetricDifference(a, b)
--> {"Serena", "Jim"}
end run
-- GENERIC FUNCTIONS ----------------------------------------------
-- delete :: Eq a => a -> [a] -> [a]
on |delete|(x, xs)
set mbIndex to elemIndex(x, xs)
set lng to length of xs
if mbIndex is not missing value then
if lng > 1 then
if mbIndex = 1 then
items 2 thru -1 of xs
else if mbIndex = lng then
items 1 thru -2 of xs
else
tell xs to items 1 thru (mbIndex - 1) & ¬
items (mbIndex + 1) thru -1
end if
else
{}
end if
else
xs
end if
end |delete|
-- difference :: [a] -> [a] -> [a]
on difference(xs, ys)
script
on |λ|(a, y)
if a contains y then
my |delete|(y, a)
else
a
end if
end |λ|
end script
foldl(result, xs, ys)
end difference
-- elemIndex :: a -> [a] -> Maybe Int
on elemIndex(x, xs)
set lng to length of xs
repeat with i from 1 to lng
if x = (item i of xs) then return i
end repeat
return missing value
end elemIndex
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- nub :: [a] -> [a]
on nub(xs)
if (length of xs) > 1 then
set x to item 1 of xs
[x] & nub(|delete|(x, items 2 thru -1 of xs))
else
xs
end if
end nub
-- union :: [a] -> [a] -> [a]
on union(xs, ys)
script flipDelete
on |λ|(xs, x)
my |delete|(x, xs)
end |λ|
end script
set sx to nub(xs)
sx & foldl(flipDelete, nub(ys), sx)
end union
{"Serena", "Jim"}
on symmetricDifference(a, b)
set output to {}
repeat 2 times
repeat with thisItem in a
set thisItem to thisItem's contents
tell {thisItem}
if (not ((it is in b) or (it is in output))) then set end of output to thisItem
end tell
end repeat
set {a, b} to {b, a}
end repeat
return output
end symmetricDifference
on task()
set a to {"John", "Serena", "Bob", "Mary", "Serena"}
set b to {"Jim", "Mary", "John", "Jim", "Bob"}
return symmetricDifference(a, b)
end task
task()
{"Serena", "Jim"}
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
on symmetricDifference(a, b)
set a to current application's class "NSSet"'s setWithArray:(a)
set b to current application's class "NSMutableSet"'s setWithArray:(b)
set output to a's mutableCopy()
tell output to minusSet:(b)
tell b to minusSet:(a)
tell output to unionSet:(b)
return output's allObjects() as list
end symmetricDifference
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
on symmetricDifference(a, b)
set a to current application's class "NSSet"'s setWithArray:(a)
set b to current application's class "NSMutableSet"'s setWithArray:(b)
set output to a's mutableCopy()
tell output to unionSet:(b)
tell b to intersectSet:(a)
tell output to minusSet:(b)
return output's allObjects() as list
end symmetricDifference
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
on symmetricDifference(a, b)
set unionArray to (current application's class "NSArray"'s arrayWithArray:({a, b}))'s ¬
valueForKeyPath:("@distinctUnionOfArrays.self")
set filter to current application's class "NSPredicate"'s ¬
predicateWithFormat_("!((self IN %@) && (self IN %@))", a, b)
return (unionArray's filteredArrayUsingPredicate:(filter)) as list
end symmetricDifference
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the Maple programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Scala programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the Ruby programming language
You may also check:How to resolve the algorithm Hostname step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the True BASIC programming language