Symmetric difference: Difference between revisions

m
First example shows symmetric difference w/o using `><` operator
m (sntax highlighting fixup automation)
m (First example shows symmetric difference w/o using `><` operator)
 
(18 intermediate revisions by 11 users not shown)
Line 377:
<pre>
Serena Jim
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <basico.h>
 
algoritmo
matrices (A, B)
"John", "Bob", "Mary", "Serena", enlistar en 'A'
"Jim", "Mary", "John", "Bob", enlistar en 'B'
" A XOR B: {", diferencia simétrica(A,B), "}\n"
" A \ B: {", diferencia(A,B), "}\n"
" B \ A: {", diferencia(B,A), "}\n"
imprimir
terminar
</syntaxhighlight>
{{out}}
<pre>
A XOR B: {Jim,Serena}
A \ B: {Serena}
B \ A: {Jim}
</pre>
 
Line 422 ⟶ 446:
 
=={{header|AppleScript}}==
===Functional===
{{Trans|JavaScript}} (ES6 Functional JS)
<syntaxhighlight lang="applescript">-- SYMMETRIC DIFFERENCE -------------------------------------------
Line 537 ⟶ 562:
{{Out}}
<syntaxhighlight lang="applescript">{"Serena", "Jim"}</syntaxhighlight>
===Some alternatives===
There are several ways to approach this problem. Vanilla AppleScript doesn't do sets, but the following returns the required results:
<syntaxhighlight lang="applescript">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()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">{"Serena", "Jim"}</syntaxhighlight>
 
AppleScriptObjC gives access to the Foundation framework's NSSet classes, whose mutable variety has relevant methods. A symmetricDifference() handler implementing (A ∖ B) ∪ (B ∖ A) might be:
 
<syntaxhighlight lang="applescript">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</syntaxhighlight>
 
And for (A ∪ B) ∖ (A ∩ B):
 
<syntaxhighlight lang="applescript">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</syntaxhighlight>
 
It's also possible in ASObjC to filter arrays or sets if you're so inclined:
 
<syntaxhighlight lang="applescript">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</syntaxhighlight>
 
=={{header|Arturo}}==
Line 1,080 ⟶ 1,180:
ReadLn;
END.</syntaxhighlight>
'''Delphi/Object Pascal actually has a 'Symmetric Difference' operator `> <`: '''
<syntaxhighlight lang="pascal">
program SymmetricDifference;
 
type
charSet = set of Char;
 
var
s1, s2, s3: charSet;
ch: char;
 
begin
s1 := ['a', 'b', 'c', 'd'];
s2 := ['c', 'd', 'e', 'f'];
s3 := s1 >< s2;
 
for ch in s3 do
write(ch, ' ');
writeLn;
end.
</syntaxhighlight>
Output:
<pre>
a b e f
</pre>
 
=={{header|Déjà Vu}}==
Line 1,106 ⟶ 1,231:
? symmDiff(["John", "Bob", "Mary", "Serena"].asSet(), ["Jim", "Mary", "John", "Bob"].asSet())
# value: ["Jim", "Serena"].asSet()</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
a$[] = [ "John" "Bob" "Mary" "Serena" ]
b$[] = [ "Jim" "Mary" "John" "Bob" ]
#
for i to 2
for a$ in a$[]
for b$ in b$[]
if a$ = b$
a$ = ""
break 1
.
.
if a$ <> ""
c$[] &= a$
.
.
swap a$[] b$[]
.
print c$[]
</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 1,328 ⟶ 1,475:
A - B : [Serena]
B - A : [Jim]
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn SymmetricDifferenceOfSets( setA as CFSetRef, setB as CFSetRef ) as CFSetRef
CFMutableSetRef notInSetA = fn MutableSetWithSet( setB )
MutableSetMinusSet( notInSetA, setA )
CFMutableSetRef notInSetB = fn MutableSetWithSet( setA )
MutableSetMinusSet( notInSetB, setB )
CFMutableSetRef symmetricDifference = fn MutableSetWithSet( notInSetA )
MutableSetUnionSet( symmetricDifference, notInSetB )
end fn = fn SetWithSet( symmetricDifference )
 
CFSetRef set1, set2
 
set1 = fn SetWithObjects( @"John", @"Serena", @"Bob", @"Mary", @"Serena", NULL )
set2 = fn SetWithObjects( @"Jim", @"Mary", @"John", @"Jim", @"Bob", NULL )
 
NSLog( @"Symmetric difference:\n%@", fn SymmetricDifferenceOfSets( set1, set2 ) )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Symmetric difference:
{(
Jim,
Serena
)}
</pre>
 
Line 2,247 ⟶ 2,425:
to_sorted_list(Set, Elems),
io.format("%11s: %s\n", [s(Desc), s(string(Elems))], !IO).</syntaxhighlight>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
Set = new map
Set["set"] = {}
 
Set.init = function(items)
set = new Set
set.set = {}
for item in items
set.add(item)
end for
return set
end function
 
Set.contains = function(item)
return self.set.hasIndex(item)
end function
 
Set.items = function
return self.set.indexes
end function
 
Set.add = function(item)
self.set[item] = true
end function
 
Set.union = function(other)
result = Set.init
result.set = self.set + other.set
return result
end function
 
Set.difference = function(other)
result = Set.init
for item in self.items
if not other.contains(item) then result.add(item)
end for
return result
end function
 
Set.symmetricDifference = function(other)
diff1 = self.difference(other)
diff2 = other.difference(self)
return diff1.union(diff2)
end function
 
a = ["John", "Serena", "Bob", "Mary", "Serena"]
b = ["Jim", "Mary", "John", "Jim", "Bob"]
 
A1 = Set.init(a)
B1 = Set.init(b)
 
print "A XOR B " + A1.symmetricDifference(B1).items
print "A - B " + A1.difference(B1).items
print "B - A " + B1.difference(A1).items
</syntaxhighlight>
{{out}}
<pre>A XOR B ["Serena", "Jim"]
A - B ["Serena"]
B - A ["Jim"]
</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda>main :: [sys_message]
main = [Stdout (show (symdiff a b) ++ "\n")]
where a = ["John", "Serena", "Bob", "Mary", "Serena"]
b = ["Jim", "Mary", "John", "Jim", "Bob"]
 
symdiff :: [*]->[*]->[*]
symdiff a b = (a' -- b') ++ (b' -- a')
where a' = nub a
b' = nub b
 
nub :: [*]->[*]
nub = f []
where f acc [] = acc
f acc (a:as) = f acc as, if a $in acc
= f (a:acc) as, otherwise
 
in :: *->[*]->bool
in i [] = False
in i (a:as) = a == i \/ i $in as</syntaxhighlight>
{{out}}
<pre>["Serena","Jim"]</pre>
 
=={{header|Nim}}==
Line 2,412 ⟶ 2,675:
Put('ListA -> ', ListA);
Put('ListB -> ', ListB);
Put('ListA >< ListB -> ', (ListA ><- ListB) + (ListB - ListA));
Put('ListA - ListB -> ', ListA - ListB);
Put('ListB - ListA -> ', ListB - ListA);
Line 2,423 ⟶ 2,686:
ListA - ListB -> Serena
ListB - ListA -> Jim</pre>
'''Object Pascal actually has a 'Symmetric Difference' operator `> <`: '''
<syntaxhighlight lang="pascal">
program SymmetricDifference;
 
type
charSet = set of Char;
 
var
s1, s2, s3: charSet;
ch: char;
 
begin
s1 := ['a', 'b', 'c', 'd'];
s2 := ['c', 'd', 'e', 'f'];
s3 := s1 >< s2;
 
for ch in s3 do
write(ch, ' ');
writeLn;
end.
</syntaxhighlight>
Output:
<pre>
a b e f
</pre>
 
=={{header|Perl}}==
Line 2,914 ⟶ 3,202:
</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, John Bob Mary Serena: e.A
, Jim Mary John Bob: e.B
= <Prout <Symdiff (e.A) (e.B)>>;
};
 
Symdiff {
(e.1) (e.2), <Diff (<Set e.1>) (<Set e.2>)>: e.3
, <Diff (<Set e.2>) (<Set e.1>)>: e.4
= <Union (e.3) (e.4)>;
};
 
Set {
= ;
s.1 e.1 s.1 e.2 = <Set e.1 s.1 e.2>;
s.1 e.1 = s.1 <Set e.1>;
};
 
Union {
(e.1) (e.2) = <Set e.1 e.2>;
};
 
Diff {
() (e.1) = ;
(e.1) () = e.1;
(s.1 e.1) (e.2 s.1 e.3) = <Diff (e.1) (e.2 e.3)>;
(s.1 e.1) (e.2) = s.1 <Diff (e.1) (e.2)>;
};</syntaxhighlight>
{{out}}
<pre>Serena Jim</pre>
=={{header|REXX}}==
===version 1===
Line 3,048 ⟶ 3,367:
see blist2 see nl
</syntaxhighlight>
 
=={{header|RPL}}==
<code>DIFFL</code> is defined at [[Set#RPL|Set]]
{{works with|Halcyon Calc|4.2.9}}
≪ DUP2 <span style="color:blue>DIFFL</span>
ROT ROT SWAP <span style="color:blue>DIFFL</span> +
≫ ≫ '<span style="color:blue>SYMDIFF</span>' STO
 
{"John" "Bob" "Mary" "Serena"} {"Jim" "Mary" "John" "Bob"} <span style="color:blue>SYMDIFF</span>
{{out}}
<pre>
1: {"Jim" "Serena"}
</pre>
 
=={{header|Ruby}}==
Line 3,223 ⟶ 3,555:
{Jim, Serena}
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program symmetric_difference;
A := {"John", "Bob", "Mary", "Serena"};
B := {"Jim", "Mary", "John", "Bob"};
 
print("A - B:", A - B);
print("B - A:", B - A);
print("Symmetric difference:", (A-B) + (B-A));
end program;</syntaxhighlight>
{{out}}
<pre>A - B: {Serena}
B - A: {Jim}
Symmetric difference: {Jim Serena}</pre>
 
=={{header|Sidef}}==
Line 3,417 ⟶ 3,763:
'b not a': <'Jim'>,
'symmetric difference': <'Jim','Serena'>></pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">
const
(
alist = ["john", "bob", "mary", "serena"]
blist = ["jim", "mary", "john", "bob"]
)
 
fn main() {
mut rlist := []string{}
for elem in alist {
if blist.any(it == elem) == false {
println("a - b = $elem")
rlist << elem
}
}
for elem in blist {
if alist.any(it == elem) == false {
println("b - a = $elem")
rlist << elem
}
}
println("symmetric difference: $rlist")
}
</syntaxhighlight>
 
{{out}}
<pre>
a - b = serena
b - a = jim
symmetric difference: ['serena', 'jim']
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-set}}
<syntaxhighlight lang="ecmascriptwren">import "./set" for Set
 
var symmetricDifference = Fn.new { |a, b| a.except(b).union(b.except(a)) }
57

edits