Common list elements: Difference between revisions

→‎{{header|AppleScript}}: Separated the sort from the handler for flexibility. Added a solution using only core AppleScript.
m (→‎{{header|AppleScript}}: Posting tag correction.)
(→‎{{header|AppleScript}}: Separated the sort from the handler for flexibility. Added a solution using only core AppleScript.)
Line 19:
 
=={{header|AppleScript}}==
===AppleScriptObjC===
<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use sorter : script "Insertion Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript>
 
on commonListElements(listOfNumberListslistOfLists)
set mutableSet to current application's class "NSMutableSet"'s setWithArray:(beginning of listOfNumberListslistOfLists)
repeat with i from 2 to (count listOfNumberListslistOfLists)
tell mutableSet to intersectSet:(current application's class "NSSet"'s setWithArray:(item i of listOfNumberListslistOfLists))
end repeat
set sortDescriptor to current application's class "NSSortDescriptor"'s sortDescriptorWithKey:("self") ¬
ascending:(true)
return (mutableSet's sortedArrayUsingDescriptors:allObjects({sortDescriptor})) as list
end commonListElements
 
set commonElements to commonListElements({{2, 5, 1, 3, 8, 9, 4, 6}, {3, 5, 6, 2, 9, 8, 4}, {1, 3, 7, 6, 9}})</lang>
tell sorter to sort(commonElements, 1, -1)
return commonElements</lang>
 
{{output}}
<lang applescript>{3, 6, 9}</lang>
 
===Core language only===
The requirement for AppleScript 2.3.1 is only for the 'use' command which loads the "Insertion Sort" script. If the sort's instead loaded with the older 'load script' command or copied into the code, this will work on systems as far back as Mac OS X 10.5 (Leopard) or earlier. Same output as above.
<lang applescript>use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later.
use sorter : script "Insertion Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript>
 
on commonListElements(listOfLIsts)
script o
property list1 : beginning of listOfLIsts
end script
set common to {}
set listCount to (count listOfLIsts)
repeat with i from 1 to (count o's list1)
set thisElement to {item i of o's list1}
if (thisElement is not in common) then
repeat with j from 2 to listCount
set OK to (item j of listOfLIsts contains thisElement)
if (not OK) then exit repeat
end repeat
if (OK) then set end of common to beginning of thisElement
end if
end repeat
return common
end commonListElements
 
set commonElements to commonListElements({{2, 5, 1, 3, 8, 9, 4, 6}, {3, 5, 6, 2, 9, 8, 4}, {1, 3, 7, 6, 9}})
tell sorter to sort(commonElements, 1, -1)
return commonElements</lang>
 
=={{header|AutoHotkey}}==
557

edits